我想做一些我认为是使用“咖啡脚本类”和 Angular JS 结构的好方法。
<!doctype html>
<html ng-app>
<head>
<meta charset=utf-8>
<meta name=viewport content="width=device-width, initial-scale=1">
<title>Main Test</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="js/coffee.js"></script>
</head>
<body>
<div ng-controller="MainClass" style="margin-left:10px">
<h2>All todos: {{test()}}</h2>
</div>
</body>
</html>
请注意,我将 DIV ng-controller 设置为 MainClass 并在 H2 HTML 标记内绑定 test() 方法。
class AngularJSController
constructor: ($scope, $main) ->
$scope.test = MainClass.prototype.test
MainClass.test = MainClass.prototype.test
$main.test = MainClass.prototype.test
test = MainClass.prototype.test
@test = MainClass.prototype.test
console.log @test
class MainClass extends AngularJSController
constructor: ($scope) ->
super $scope, this
setTimeout (->
console.log test()
), 1000
test();
test: -> 'aloha!'
在 AngularJSController 构造函数中,我尝试了所有我想在 MainClass 范围内设置我的超类方法 TEST 的形式,但没有成功。
我正在尝试这样做,因为我只想在我的 Angular JS 控制器和组件上使用类。
我已经遇到的问题:
如果我尝试使用
@test()
而不是test()
在 setTimeout 中使用,jQuery 已经用this
一种 JQuery Window 对象替换了该属性。setTimeout (-> console.log @test()), 1000
我不知道
test()
调用的真正范围是什么,如果这个地方(或@cause Coffee)与地方任何东西都不一样。test() != this.test() != @.test() # the first scope isn't the same scope of last two calls