这是我正在使用的确切指令:
'use strict';
angular.module('App')
.directive('aView', function ($stateParams) {
this.link = function(scope, template, directiveAttrs){
template.addClass(scope.elem.classes);
}
return {
template: '<div ng-transclude></div>',
restrict: 'E',
replace: true,
scope: {elem: '='},
compile: function(template){
return function(scope, template, directiveAttrs){
template.addClass(scope.elem.classes);
}
}
}
});
这给了我以下错误:
TypeError: Cannot set property 'link' of undefined
at http://localhost:9000/scripts/directives/aView.js:5:15
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:2990:25)
at http://localhost:9000/bower_components/angular/angular.js:3894:43
at Array.forEach (native)
at forEach (http://localhost:9000/bower_components/angular/angular.js:130:11)
at Object.<anonymous> (http://localhost:9000/bower_components/angular/angular.js:3892:13)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:3000:28)
at http://localhost:9000/bower_components/angular/angular.js:2838:37
at Object.getService [as get] (http://localhost:9000/bower_components/angular/angular.js:2960:39)
at addDirective (http://localhost:9000/bower_components/angular/angular.js:4609:51)
关于这个的奇怪之处在于,在该指令的返回配置对象中,我没有引用 this.link() 函数。另外为什么我变得不确定?我的指令几乎完全复制了 agghead 第 25 课。
现在,如果我注释掉 this.link() 函数,就会出现一组错误:
10 x :
TypeError: undefined is not a function
at new ngDirective.controller (http://localhost:9000/bower_components/angular/angular.js:14357:5)
at invoke (http://localhost:9000/bower_components/angular/angular.js:3000:28)
at Object.instantiate (http://localhost:9000/bower_components/angular/angular.js:3012:23)
at http://localhost:9000/bower_components/angular/angular.js:4981:24
at http://localhost:9000/bower_components/angular/angular.js:4560:17
at forEach (http://localhost:9000/bower_components/angular/angular.js:137:20)
at nodeLinkFn (http://localhost:9000/bower_components/angular/angular.js:4545:11)
at compositeLinkFn (http://localhost:9000/bower_components/angular/angular.js:4191:15)
at compositeLinkFn (http://localhost:9000/bower_components/angular/angular.js:4194:13)
at publicLinkFn (http://localhost:9000/bower_components/angular/angular.js:4096:30) angular.js:5930
Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [a lot of code here]...
...debug.watchPerf[watchStr].calls += 1;\n return ret;\n }; newVal: undefined; oldVal: undefined"]]
at Error (<anonymous>)
at Object.Scope.$digest (http://localhost:9000/bower_components/angular/angular.js:8126:19)
at Object.$delegate.__proto__.$digest (<anonymous>:844:31)
at Object.Scope.$apply (http://localhost:9000/bower_components/angular/angular.js:8304:24)
at Object.$delegate.__proto__.$apply (<anonymous>:855:30)
at http://localhost:9000/bower_components/angular/angular.js:9669:36
at completeOutstandingRequest (http://localhost:9000/bower_components/angular/angular.js:3139:10)
at http://localhost:9000/bower_components/angular/angular.js:3433:7
到底是怎么回事?是否存在对我的 this.link 函数的神秘依赖出现在 Angular 中?
顺便说一句,这个函数的名称无关紧要,错误仍然会以不同的名称作为参考出现。
BTW2,我的堆栈是:yeoman(作为本地服务器的 Grunt)、angular.js、angular.ui.bootstrap、angular.ui.router、Firebase、angularFire。项目是用 yo 角度发生器制作的。
顺便说一句,在我开始修改这个指令之前,最初使用 yo angular:directive 生成的指令有一个链接:function postLink(scope, element, attrs)如果这很重要。
感谢你的时间,
贾里德