Angular app only loads on page refresh

2019-08-31 04:24发布

问题:

I am building a wordpress site with a page that is an angular app. It was working until recently (and I can't recall which change broke it), but now the app does not load when I arrive at the page via a link (angular is loaded and the code snippet below processed, but nothing is logged), but it does work if I then refresh the page.

I've backed out all the real code to leave:

angular.module("af2015App", [])
.controller("TestCtrl", function() {
    console.log("test test");
});

And commented out everything in the HTML except

<div ng-app="af2015App" class="app">
    <p ng-controller="TestCtrl">Use the ...!</p>
</div>

I'm at a loss as to how to debug these circumstances. I'm not loading pages via AJAX.

回答1:

Thanks for your ideas everyone. I had another go at debugging and discovered that, even after turning off the script tag with angular, angular was still present. That gave me the idea to look at Batarang and, after deactivating that, all is working again. Perhaps this answer will help someone someday. Needless to say I don't know what the underlying problem was.



回答2:

take a look at this plunker working plunker

they only thing I can think of when looking at your code is that you are not closing your div tag altough that is probably just an edit mistake otherwise move your ng-app definition to your html tag

    <html ng-app="af2015App">
      <head> 
         //make sure your scripts are loaded correctly

         <script src="path/to/angular.js"></script>
         <script src="path/to/app.js"></script>
         <script src="path/to/controller.js"></script>
      </head>
        <div class="app">
            <p ng-controller="TestCtrl">Use the ...!</p>
        </div>
    </html>