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

拈花ヽ惹草 提交于 2019-12-02 07:51:57

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)

<body 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

<html>
  <head>
    <script src="http://requirejs.org/docs/release/2.1.14/minified/require.js" data-main="main.js"></script>
  </head>
  <body>
      <div ng-controller="HomeController">
        {{message}}
    </div>
  </body>
</html>

Yea, me too took long time to figured it out.

This is mainly because requirejs won't register all the controllers initially to the app since it is lazily loading the files and the controllers

The things you need to do is, if you want to call any of the controller, you need to register manually with the modules. So you won't get the issue.

Example

       app.config(function($routeProvider, $locationProvider,$controllerProvider) {

            //To register the controller which will be load on demand (which will be loaded lazily)
            app.registerController = $controllerProvider.register;

            // To register the following which will be loaded lazily later for future use
            // * Constant   -   app.regiser.value     -   To register constant
            // * Factory    -   app.regiser.factory   -   To register factory
            // * service    -   app.regiser.service   -   To register service
            app.$register = $provide;

            // To register filter
            app.registerFilter = $filterProvider.register;

            // To register directive which will be loaded lazily
            app.registerDirective = $compileProvider.directive;
        }


        app.registerController("HomeController", function ($scope) {
                $("body").append("hello from home controller body<br/>");
                $scope.message = "message from home controller";
            });

var app = angular.module("app");

        app.$register.factory('appService',function(){

        });

Or You can go with some third party plugins like AngularAMD, $couchPotatoProvider.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!