简体   繁体   English

添加指令时如何避免多次角度加载?

[英]How to avoid angular loading multiple times when adding directive?

In my app.js file I have the app being created like this: 在我的app.js文件中,我创建的应用程序是这样的:

angular
    .module('AngularRails',[
      'ngRoute',
      'templates',
      'firebase',
      'mySharedElements',])

i'm attempting to load a directive in one of my views like this: 我正在尝试在我的一种视图中加载指令,如下所示:

angular.module("mySharedElements", []).directive('userItem', [function() 

However, this doesn't work? 但是,这行不通吗? I receive an error in Developer Tools stating that Angular attempted to load multiple times and thus the tab locks up. 我在开发人员工具中收到一条错误消息,指出Angular attempted to load multiple times ,因此选项卡被锁定。 I tried solving that but adding 'mySharedElements' as you can see from the code but that didn't work. 我尝试解决此问题,但是添加了“ mySharedElements”,如您从代码中看到的那样,但这没有用。 From various sites I've read, I'm doing it the right way so I'm confused what I'm doing wrong because obviously I am. 在我读过的各个网站上,我都在以正确的方式做事,所以我很困惑自己做错了什么,因为显然我是。

Thank you for your help, I appreciate it. 谢谢您的帮助,不胜感激。 :) :)

UPDATE: 更新:

I double checked and i'm not using ng-app more than once. 我仔细检查过,但我没有多次使用ng-app。 The error only occurs when I attempt to go to the page where the directive is being used. 仅当我尝试转到使用该指令的页面时,才会发生该错误。

I update the app.js file and the directive to this to see if that fixed it: 我更新了app.js文件和对此的指令,以查看是否可以解决该问题:

app.js: app.js:

angular
    .module('AngularRails',[
      'ngRoute',
      'templates',
      'firebase',])
    .config(function ($routeProvider, $locationProvider) {
// etc.

directive: 指示:

angular.module("AngularRails").directive('userItem', [function() {
// etc.

And that fixed it. 这样就解决了。 It looks like I was suffering from a combination of this and I had the templateURL wrong in the directive and it was screwing stuff up. 似乎我受此困扰,指令中的templateURL错误,并且搞砸了。 THANKS! 谢谢!

angular.module("mySharedElements", []) will create a new app because you are passing second argument to module with empty dependencies. angular.module("mySharedElements", [])将创建一个新应用,因为您将第二个参数传递给具有空依赖项的模块。

If you have already created the app somewhere else in your code then you just need a reference to it. 如果您已经在代码中的其他位置创建了该应用程序,则只需引用它即可。 Change it to angular.module("mySharedElements").directive then it should be fine. 将其更改为angular.module("mySharedElements").directive

However looking at the error description mentioned in the question Angular attempted to load multiple times , it looks like you have multiple references to angular js on your page. 但是,查看Angular attempted to load multiple times问题中提到的错误描述,您似乎在页面上有多个对angular js的引用。 Try to check for that and remove multiple references. 尝试检查并删除多个引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM