angular.module meaning of the second parameter “requires”

前端 未结 2 640
离开以前
离开以前 2021-01-03 03:43

Recall the method signature for angular.module. If the second parameter, requires is provided, then we are creating a new module instead of retrieving an existi

相关标签:
2条回答
  • 2021-01-03 04:05

    The second parameter is used to define the module's dependencies - i.e., a list of modules (module names, to be precise) that should be already loaded by the injector before the current module is loaded.

    And here's how this param (stored in the module's requires property) is used: (injector.js/loadModules()):

    var runBlocks = [], moduleFn, invokeQueue, i, ii;
    forEach(modulesToLoad, function(module) {
      if (loadedModules.get(module)) return; // skipping already loaded modules
      loadedModules.put(module, true);
    
      if (isString(module)) {
        moduleFn = angularModule(module); // prepared module object
        runBlocks = runBlocks.concat(loadModules(moduleFn.requires))
                             .concat(moduleFn._runBlocks);
        // ...
      }
      // ...
    }
    return runBlocks;
    

    As you see, this property can be used to set up a hierarchy of dependencies as well (when ModuleFoo depends on ModuleBar depending on ModuleBaz).

    0 讨论(0)
  • 2021-01-03 04:22

    requires meaning an array of modules which your module depends.

    example:

    moduleA.js

    var customModule = angular.module ('ModuleA');
    // controller, services, factories , etc codes here
    

    app.js (main app)

    var app = angular.module ("app", ["ModuleA"]);
    

    if I just use:

    angular.module ("app");
    

    It means that i'm just retrieving the module named "app". Which is useable when controllers or directives or factories is defined in a different JS files and you want to configure it to the module "app"

    0 讨论(0)
提交回复
热议问题