我无法在我的AngularJS + Coffeescript项目中制作最简单的指令。

我有这个代码.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>'
.

和在我的模板中,当我做

<span app-version></span>
<hello></hello>
.

然后显示版本号(0.1),显示第一个指令正常工作,但标签不会被任何内容替换。

任何想法我做错了什么?

我也尝试过这个,它不起作用:

# Hello world directive
app.directive 'hello', ->
    class Habit
        constructor: ->
            restict: 'E'
            template: '<div>Hello World</div>'
.

有帮助吗?

解决方案

你有一个错字:

restict: 'E'
.

应该是

restrict: 'E'
.

工作代码: http://plnkr.co/edit/8tifps2emyplo4wl7ytk?p=preview

其他提示

您还可以在CoffeeScript中编写您的角度指令,如我认为更清洁:

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)
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top