AngularJS + Coffeescript - 'Hello World'指令不起作用

mas*_*cip 2 javascript coffeescript angularjs angularjs-directive

我不能让最简单的指令在我的AngularJS + Coffeescript项目中起作用.

我在directives.coffee中有这个代码:

'use strict'
app_name = "myApp"
app = angular.module "#{app_name}.directives", []

# Directive to include the version number of my project
app.directive 'appVersion', [
'version', (version) ->
    (scope, element, attrs) ->
    element.text version
]

# Hello world directive
app.directive 'hello', () ->
    restict: 'E'
    template: '<div>Hello World</div>'
Run Code Online (Sandbox Code Playgroud)

在我的模板中,当我这样做时

<span app-version></span>
<hello></hello>
Run Code Online (Sandbox Code Playgroud)

然后显示版本号(0.1),表明第一个指令正常工作,但标签不会被任何东西取代.

知道我做错了什么吗?

我也试过这个,但也没用:

# Hello world directive
app.directive 'hello', ->
    class Habit
        constructor: ->
            restict: 'E'
            template: '<div>Hello World</div>'
Run Code Online (Sandbox Code Playgroud)

Mar*_*n K 7

您也可以在CoffeeScript中编写Angular 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)

  • 嗨@virtualandy.你可以注入这样的服务:angular.module('my_module').指令'MyDirective',($ http) - > new MyDirective($ http) (2认同)