Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Angular尝试使用/访问尚未';未在指令'中使用;s返回对象_Angularjs_Angularjs Directive - Fatal编程技术网

Angularjs Angular尝试使用/访问尚未';未在指令'中使用;s返回对象

Angularjs Angular尝试使用/访问尚未';未在指令'中使用;s返回对象,angularjs,angularjs-directive,Angularjs,Angularjs Directive,以下是我使用的确切指令: 'use strict'; angular.module('App') .directive('aView', function ($stateParams) { this.link = function(scope, template, directiveAttrs){ template.addClass(scope.elem.classes); } return { template: '<div ng-

以下是我使用的确切指令:

'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);
        }
      }
    }
  });
“严格使用”;
角度。模块('App')
.directive('aView',function($stateParams){
this.link=函数(作用域、模板、directiveAttrs){
template.addClass(scope.elem.classes);
}
返回{
模板:“”,
限制:'E',
替换:正确,
作用域:{elem:'='},
编译:函数(模板){
返回函数(作用域、模板、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) 
TypeError:无法设置未定义的属性“link”
在http://localhost:9000/scripts/directives/aView.js:5:15
在Object.invoke(http://localhost:9000/bower_components/angular/angular.js:2990:25)
在http://localhost:9000/bower_components/angular/angular.js:3894:43
at Array.forEach(本机)
在弗雷赫(http://localhost:9000/bower_components/angular/angular.js:130:11)
反对。(http://localhost:9000/bower_components/angular/angular.js:3892:13)
在Object.invoke(http://localhost:9000/bower_components/angular/angular.js:3000:28)
在http://localhost:9000/bower_components/angular/angular.js:2838:37
在Object.getService[as get](http://localhost:9000/bower_components/angular/angular.js:2960:39)
至少(http://localhost:9000/bower_components/angular/angular.js:4609:51) 
这条指令的奇怪之处在于,在该指令返回的配置对象中,我没有引用this.link()函数。还有,为什么我没有定义?我的指令几乎完全是阿格海德第25课的副本

现在,如果我注释掉这个.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 
10x:
TypeError:undefined不是函数
在新的ngDirective.controller上(http://localhost:9000/bower_components/angular/angular.js:14357:5)
在调用时(http://localhost:9000/bower_components/angular/angular.js:3000:28)
在Object.instantiate(http://localhost:9000/bower_components/angular/angular.js:3012:23)
在http://localhost:9000/bower_components/angular/angular.js:4981:24
在http://localhost:9000/bower_components/angular/angular.js:4560:17
在弗雷赫(http://localhost:9000/bower_components/angular/angular.js:137:20)
在诺德琳(http://localhost:9000/bower_components/angular/angular.js:4545:11)
在KFN(http://localhost:9000/bower_components/angular/angular.js:4191:15)
在KFN(http://localhost:9000/bower_components/angular/angular.js:4194:13)
在publicLinkFn(http://localhost:9000/bower_components/angular/angular.js:4096:30)www.js:5930
错误:已达到10$digest()迭代。流产!
观察者在过去5次迭代中开火:[这里有很多代码]。。。
…debug.watchPerf[watchStr]。调用+=1\n返回ret\n};newVal:未定义;oldVal:未定义“]]
错误()
位于Object.Scope.$digest(http://localhost:9000/bower_components/angular/angular.js:8126:19)
在Object.$delegate.\uuuu proto.$digest(:844:31)
在Object.Scope.$apply(http://localhost:9000/bower_components/angular/angular.js:8304:24)
在对象处。$delegate.\uuuu proto.$apply(:855:30)
在http://localhost:9000/bower_components/angular/angular.js:9669:36
在完成未完成的请求时(http://localhost:9000/bower_components/angular/angular.js:3139:10)
在http://localhost:9000/bower_components/angular/angular.js:3433:7 
发生了什么?angular中是否出现了对我的this.link函数的神秘依赖?
顺便说一句,无论此函数的名称如何,错误仍然会以不同的名称作为参考出现。
BTW2,我的堆栈是:yeoman(Grunt作为本地服务器)、angular.js、angular.ui.bootstrap、angular.ui.router、Firebase、angularFire。项目由yo angular generator制作。
BTW3,在我开始修改该指令之前,最初使用yo angular:directive生成的指令有一个link:function postLink(范围、元素、属性),如果这很重要的话。
感谢您的时间,

Jared

通常在指令中,
窗口
,但由于您使用的是严格模式,它被更改为
未定义的
。这是为了防止人们在使用构造函数模式时意外修改全局对象,而不使用
新建
关键字

如果删除
“use strict”
您将不会看到任何错误,但是如果删除
console.log(this)
您将看到一个窗口,您不应该添加这样的方法。我建议您这样做

var link = function() { ... };

通常在指令中,
窗口
,但由于您使用的是严格模式,因此将其更改为
未定义的
。这是为了防止人们在使用构造函数模式时不使用
新建
关键字而意外修改全局对象

如果删除
“use strict”
您将不会看到任何错误,但是如果删除
console.log(this)
您将看到一个窗口,您不应该添加这样的方法。我建议您这样做

var link = function() { ... };

它成功了,干杯。但是为什么当我注释掉函数时会出现另一组错误呢?也许你需要重命名
模板中的一个,你不应该让它们命名相同,是的,我将“元素”重命名为“模板”,因为我总是对它所指的“元素”感到困惑。我一直在使用这个名称在好几个地方,我都没有遇到任何问题,但我会把它改成类似templateElem的东西,只是为了确定:)。它成功了,干杯。但是为什么在我注释掉函数时会出现另一组错误呢?也许你需要重命名其中一个
模板
你不应该让它们都命名为同一个,是的,我重命名了'e'