Pergunta

Eu não posso fazer a mais simples das directivas trabalhar na minha AngularJS + Coffeescript projeto.

Eu tenho esse código em directivas.café:

'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>'

E no meu modelo, quando eu faço

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

em seguida, o número da versão aparece (0.1), mostrando que a primeira directiva funciona corretamente, mas a marca não é substituído por nada.

Qualquer idéia que eu fiz de errado?

Eu também tentei fazer isso, que não quer trabalhar:

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

Solução

Você tem um erro de digitação:

restict: 'E'

deve ser

restrict: 'E'

Código de trabalho: http://plnkr.co/edit/8TifpS2EmYPLo4wl7YtK?p=preview

Outras dicas

Você também pode escrever seu Angular Directiva em CoffeeScript como este que eu acho que é cleaner:

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)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top