WARNING: Tried to load angular more than once. Angular JS

后端 未结 23 1300
有刺的猬
有刺的猬 2020-12-01 13:57

I am trying to view my app after running Grunt Build. I use grunt serve:dist to see all production ready build but in the browser I get an infinite loop saying:

相关标签:
23条回答
  • 2020-12-01 14:16

    Yeah I sorted it out by moving the post.html into partials and changing the templateUrl to partials/posts.html. I think this might be due to the Yo scaffold I used which was angular fullstack, because it work fine of the see project. Thanks anyway

    0 讨论(0)
  • 2020-12-01 14:16

    In my case the problem was related to AngularJS Batarang Chrome Extension (version 0.7.1). Once I disabled the extension the error disappeared.

    0 讨论(0)
  • 2020-12-01 14:16

    Last time this happened to me, it was a leading '/' in a route's templateUrl, which wasn't suppose to be there.

    This time though, it was a bunch of views not getting rendered into the template cache after build because I've placed those views in a sub folder of a sub folder under the views folder. Gruntfile.js wasn't tuned to pick up 3rd level folders from '/views'.

    To find out what was rendered in a yo-grunt build, you can look at .tmp/templateCache.js after the build finishes.

    I do wish AngularJS LTS's team will find a way to better handle this kind of error, with some better clue as to where the problem is.

    Hope this helps!

    0 讨论(0)
  • 2020-12-01 14:18

    In my case this was due to the following html code:

    <!doctype html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Testapp</title>
    </head>
    
    <body ng-app="testApp">
    
      <main ui-view>
    
      <script src="bower_components/jquery/jquery.js"></script>
      <script src="bower_components/angular/angular.js"></script>
      <script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
    
      <script src="scripts/main.js"></script>
    </body>
    
    </html>
    

    As you can see, the <main> is not closed. This led to my variant of 'WARNING: Tried to load angular more than once.' issue.

    0 讨论(0)
  • 2020-12-01 14:19

    In my case, angular library and my angular module code is dynamically loaded inside another application which I dont have any control. And these are loaded on click of of a button. For the first time its working fine but when user clicks 2nd time the library and other files loads again and it gives me the warning.

    WARNING: Tried to load angular more than once.

    var isInitialized = element.injector();
    if (!isInitialized) {
      angular.bootstrap(angular.element(document.getElementById('#mainDiv')), ['defaultApp']);
    }
    
    0 讨论(0)
  • 2020-12-01 14:22

    Your solution might work locally for example if your running grunt serve, but when you run a grunt build your view might not be included in the dist directory. For example if you have a view and that view is in views>templates>modules anything more than two levels wont be added when you run grunt build by default. At least that was the case for me. Check if your html file is being included into the dist directory in views. If its not add your files manually to verify that it works, then update your grunt file acordingly.

    0 讨论(0)
提交回复
热议问题