AngularJS + CoffeeScript - 「ハローワールド」ディレクティブが機能しない
-
20-12-2019 - |
質問
私は、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>'
. 解決
タイプミス:
restict: 'E'
.
は
であるべきですrestrict: 'E'
.
作業コード: http://plnkr.co/edit/8tifps2emyplo4wl7ytk?p=previewid/a>
他のヒント
あなたは私がクリーナーだと思うものと同じような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