当ng-app未设置为模块名称时,角度如何选择根/主模块?

时间:2014-12-26 13:20:09

标签: javascript angularjs

Angular documentation says that ng-app value (module name) is optional。但是没有模块名称时没有描述角度的行为?那么角度如何选择根模块?

是否会创建一个通用模块并注入其中的所有可用模块?

1 个答案:

答案 0 :(得分:3)

它将是ng而不是,它不会注入其他模块。看看angular source code for function bootstrap。因此,当加载角度并且文档准备就绪时,将调用angularInit来查找具有ng-app和模块的元素,然后调用bootstrap。如果没有定义模块,你可以参考下面的逻辑,ng作为默认模块未被移植到模块数组。

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;
  };