gpt4 book ai didi

angularjs - 在 AngularJS 指令 Controller 中使用 CoffeeScript 的类

转载 作者:行者123 更新时间:2023-12-02 22:57:51 25 4
gpt4 key购买 nike

在使用 CoffeeScript 创建 AngularJS 指令时,我使用了这种方法:

angular
.module('MyApp', [])
.directive 'myDirective', ->
restrict: 'E'
controllerAs: 'ctrl'
controller: ->
new class
constructor: ->
@value = 3

此代码适用于 Angular 1.2.14— jsbin —但 1.3.0 则不然 — jsbin 。我在控制台中没有任何错误,只是它什么也没做。看起来 Controller 是一个空对象。

最佳答案

我在这个帖子中回答了几乎相同的问题:AngularJS + Coffeescript - 'Hello World' directive not working 。我喜欢将 Angular 对象保留为正确的 CoffeeScript 类。关键是将 new Directive() 包装在功能 block 内。

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)

关于angularjs - 在 AngularJS 指令 Controller 中使用 CoffeeScript 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26611774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com