Gio*_*Gpx 2 coffeescript angularjs angularjs-directive
在使用CoffeeScript创建AngularJS指令时,我使用了这种方法:
angular
.module('MyApp', [])
.directive 'myDirective', ->
restrict: 'E'
controllerAs: 'ctrl'
controller: ->
new class
constructor: ->
@value = 3
Run Code Online (Sandbox Code Playgroud)
此代码适用于Angular 1.2.14- jsbin,但不适用于1.3.0- jsbin.我在控制台中没有任何错误,只是它什么也没做.看起来控制器是一个空对象.
我在这个帖子中回答了几乎相同的问题:AngularJS + Coffeescript - 'Hello World'指令不起作用.我喜欢将我的Angular对象保持为正确的CoffeeScript类.关键是将new Directive()内部包裹在一个功能块中.
class MyDirective
constructor: (myService) ->
// Constructor stuff
@controller = MyController
@controllerAs = 'ctrl'
restrict: 'E'
replace: true
scope:
attributeStuff: '='
link: (scope, element, attr) ->
angular.module('my_module').directive 'MyDirective', (myService) ->
new MyDirective(myService)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1833 次 |
| 最近记录: |