How to check for the existence of a module without an error being raised?

前端 未结 7 872
小蘑菇
小蘑菇 2020-12-24 01:04

In Angular 1.2, ngRoute is a separate module so you can use other community routers like ui.router instead.

I\'m writing an open-source mod

7条回答
  •  离开以前
    2020-12-24 01:55

    A much better solution is to simply do your check when the module is created. You just need a utility function to add a callback.

    //create a utility function to add a callback to object methods    
    //here we are making it a method of the underscore or lowdash object
    //but it could be added to the angular global object or anything else
    _.addCallBack = function (obj, originalMethodName, callBackMethod, context){
                var fnOriginal = obj[originalMethodName],
                    outcome;
    
                context = context || obj;
    
                obj[originalMethodName] = function () {
                    var outcome = fnOriginal.apply(this, arguments);
    
                    callBackMethod.apply(this, arguments);
    
                    return outcome;
                };
    };
    
    _.addCallBack(angular, "module", function(sModuleName, asDependencies){
        if(_.contains(asDependencies, "ngRoute")){
          //your logic here
          //just loop through if you don't use underscore or lowdash
        }
    });
    

提交回复
热议问题