Manually bootstrapping AngularJS and then getting the module

后端 未结 4 964
闹比i
闹比i 2021-02-14 00:18

Generally, I\'d do the following and there would be an ng-app in my HTML:

var myApp = angular.module(\"myApp\", []);

myApp.controller(\"AttributeCt         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-02-14 00:49

    Updated

    They updated the documentation and now it reads like this

    Each item in the array should be the name of a predefined module or a (DI annotated) function that will be invoked by the injector as a run block. See: {@link angular.module modules}


    It seems a bug.

    The way you implemented to retrieve the module is correct. Just quote it from the doc to make it clear since it may not be well-known.

    When passed two or more arguments, a new module is created. If passed only one argument, an existing module (the name passed as the first argument to module) is retrieved.

    For the problem you mentioned, long story short...

    The bootstrap function calls createInjector with the module list ['ng', ['ngLocale', function(){...}] , 'myApp'] (the last one is the module you passed in)

    function bootstrap(element, modules) {
        ...
        var injector = createInjector(modules);
    

    Inside createInjector(), it calls loadModules for each module passed in

    function createInjector(modulesToLoad) {
        forEach(loadModules(modulesToLoad), function(fn) { instanceInjector.invoke(fn || noop); });
    

    And loadModules calls angularModule, which is initialized as angularModule = setupModuleLoader(window);, which creates the object window.angular.module

    function loadModules(modulesToLoad){
        ....
        var moduleFn = angularModule(module); // triggers the error
    

    The the error occurs, since angularModule takes 2nd parameter as requires. Without it, it will throws an exception on this line (line 1148) throw Error('No module: ' + name);

    Reported: https://github.com/angular/angular.js/issues/3692

提交回复
热议问题