AngularJS - $ compile不起作用

时间:2013-06-22 11:28:32

标签: angularjs compilation

我正在尝试在AngularJS中使用$ compile。 我正在阅读如何在http://docs.angularjs.org/api/ng.$compile中使用$ compile。 在开始看到错误

后,在我的应用程序中写了它

我的代码:

@EnumeratorDirective = () ->
    restrict: 'A'
    scope:
        count: '@uiCount'

    link: (scope, element) ->
        scope.size = element.val().length

        element.bind 'keyup', () ->
            scope.size = element.val().length
            ngApply(scope)


        el = angular.element('<div class="enumerator">{{ size }} of {{ count }}</div>')
        $compile(el.contents())(scope)
        element.parent().append(el)

它的输出:

ReferenceError: $compile is not defined
at link (http://10.0.1.100:88/assets/js/app.js:697:9)
at i (http://10.0.1.100:88/assets/js/angular/angular.1.0.7.min.js:44:6)
at e (http://10.0.1.100:88/assets/js/angular/angular.1.0.7.min.js:39:307)
at http://10.0.1.100:88/assets/js/angular/angular.1.0.7.min.js:38:372
at http://10.0.1.100:88/assets/js/angular/angular.1.0.7.min.js:39:257
at new S.controller (http://10.0.1.100:88/assets/js/angular/angular.1.0.7.min.js:150:223)
at d (http://10.0.1.100:88/assets/js/angular/angular.1.0.7.min.js:28:174)
at Object.instantiate (http://10.0.1.100:88/assets/js/angular/angular.1.0.7.min.js:28:304)
at http://10.0.1.100:88/assets/js/angular/angular.1.0.7.min.js:52:239
at http://10.0.1.100:88/assets/js/angular/angular.1.0.7.min.js:43:348 <input enumerator="enumerator" ui-count="13" type="text" required="required" class="ng-isolate-scope ng-scope"> 

我错过了什么?

1 个答案:

答案 0 :(得分:2)

您需要将$compile服务注入您的指令。您可以在AngularJS here中阅读有关dependecy injection的信息。