AngularJS + Coffeescript - 'Hello World' 指令不起作用
AngularJS + Coffeescript - 'Hello World' directive not working
我无法使最简单的指令在我的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
相关文章:
- 在指令控制器中使用$attrs时出现问题
- 参数变量出现ngTable指令问题
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- 使用每次都不同的transclude重复指令
- 打开一个模态并将其链接到AngularJS中的指令
- 从html创建一个指令,该指令按类名应用函数
- 将JSON对象传递给angular指令
- 如何使用ngrepeat和双向绑定获得指令的隔离范围
- JavaScript指令不能像我想象的那样工作
- AngularJs指令,该指令创建内部有数据对象的新指令
- AngularJS指令只识别双向绑定类型
- 从控制器继承了隔离的作用域以生成可重用的指令
- AngularJS指令出错-无法读取属性'编译'的未定义
- Angular指令在alertify setContent内容中不起作用
- 对父作用域的指令更新延迟了一步
- 指令的模板必须只有一个根元素:With restrict E&替换true
- AngularJS指令单元测试中未定义的函数
- 指令加载真的很长,检查加载时间的方法
- AngularJS指令,在元素后插入HTML
- AngularJS + Coffeescript - 'Hello World' 指令不起作用