When ng-app is not configured for the module name, how can angular select the root / main module?

Angular documentation states that the value of ng-app (module name) is optional . But does not describe angular behavior when there is no module name? So how do angular choose the root module?

Does it create a common module and implement all available modules in it?

+5
source share
1 answer

it will be ng and no, it will not introduce other modules. Check out the angular source code for the bootstrap function . Therefore, when angular loads and the document is ready, angularInit will be called to find the element with ng-app and the module, and then call bootstrap . if the module is not defined, you can refer to the logic below, ng does not switch to the array of modules as the 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; }; 
+3
source

Source: https://habr.com/ru/post/1209915/


All Articles