Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When ng-app is not set to a module name, how does angular pick the root/main module?

The Angular documentation says that ng-app value (module name) is optional. But does not describe angular's behaviour when there is no module name? So how does angular pick the root module ?

Does it create a generic module and injects all the available modules in it ?

like image 778
redben Avatar asked Oct 19 '22 20:10

redben


1 Answers

it will be ng and no, it won't inject other modules. Take a look at angular source code for function bootstrap. So when angular is loaded, and document is ready, angularInit will be invoked to find the element with ng-app and module as well, then call bootstrap. if no module is defined, you may refer to the logic below, ng is unshifted to the modules array as a default module.

function bootstrap(element, modules) {
  var doBootstrap = function() {
    element = jqLite(element);

    if (element.injector()) {
      var tag = (element[0] === document) ? 'document' : startingTag(element);
      throw ngMinErr('btstrpd', "App Already Bootstrapped with this Element '{0}'", tag);
    }

    modules = modules || [];
    modules.unshift(['$provide', function($provide) {
      $provide.value('$rootElement', element);
    }]);
    modules.unshift('ng');
    var injector = createInjector(modules);
    injector.invoke(['$rootScope', '$rootElement', '$compile', '$injector', '$animate',
       function(scope, element, compile, injector, animate) {
        scope.$apply(function() {
          element.data('$injector', injector);
          compile(element)(scope);
        });
      }]
    );
    return injector;
  };
like image 75
hjl Avatar answered Oct 28 '22 22:10

hjl