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

AngularJS + Coffeescript - 'Hello World' directive not working

本文关键字:World 指令 不起作用 Hello Coffeescript AngularJS      更新时间:2023-09-26

我无法使最简单的指令在我的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>'

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

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

然后出现版本号 (0.1),表明第一个指令工作正常,但标记不会被任何内容替换。

知道我做错了什么吗?

我也试过这个,也没用:

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

你也可以像这样用 CoffeeScript 编写你的 Angular 指令,我认为它更干净:

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)

您有一个拼写错误:

restict: 'E'

应该是

restrict: 'E'

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