angular.module meaning of the second parameter “requires”

前端 未结 2 656
离开以前
离开以前 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).

提交回复
热议问题