requirejs angular doesn't seem to register controller / services / etc

前端 未结 2 849
渐次进展
渐次进展 2021-01-23 13:55

I have a very simple requirejs and angular demo app. When I run the code, it\'s as if angular doesn\'t register the homeController (even tho the file does run and outputs \"Hell

2条回答
  •  情深已故
    2021-01-23 14:56

    github demo: https://github.com/zouhenry/angular-requirejs-demo

    I have figured out the problem and hope this answer will help others out there.

    The problem is when dom is ready, angular will parse the html and will try to bind HomeController to the ng-controller directive, even before the homeController file has been loaded by requirejs.

    So what I ended up doing was removing the ng-app directive from index.html. This prevented angular from automatically binding to directives when the page is loaded (we will programmatically bind the application to the page later)

    ng-app="app">

    Since I removed ng-app from the Html itself. I need to programmatically bind the the app to html. I did this with in the Main.js

    //kickoff the app
    require(["app", "routes" ], function(){
       angular.bootstrap(document, ["app"]);
    });
    

    Last thing I did was move the require.js to the head instead of the keeping it in the body (not sure if this was necessary). This is the revised index.html

    
      
        
      
      
          
    {{message}}

提交回复
热议问题