Angularjs + Coffeescript - 'Hello World'指令不起作用
-
20-12-2019 - |
题
我无法在我的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'
.
其他提示
您还可以在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)
. 不隶属于 StackOverflow