Angular,函数在(模型)工厂中返回值



我有一个工厂,我正在为一个运行良好的模型使用,我试图在其中设置一个函数,以便在调用时返回一个值,但似乎无法完全正确。可能是语法,或者我对它的思考方式不正确。这是我的

.factory('moduleModel', function($rootScope, stateModel) {
    function ModuleModelInstance(name, callback) {
        if (currentModules[name]) {
            $log.warn(name + " module already exists.");
        } else {
            this.currentState = {};
            this.callback = callback;
            this.rootScope = $rootScope.$id;
            _.extend(currentModules, _.object([
                [name, this]
            ]));
        }
        function getModule(name) {
            if (currentModules[name]) {
                return true;
            } else {
                return undefined;
            }
        }
    };
    ModuleModelInstance.prototype = {
        //add New State to module
        addState: function(state, stateVars) {
            this.states = new stateModel(state, stateVars);
        },
        setCurrent: function(state, stateVars) {
            if (this.states.canGo(state, stateVars)) {
                this.currentState = _.object([
                    [state, stateVars]
                ]);
                return true;
            } else {
                return false;
            }
        }
    };
    return ModuleModelInstance;
})

现在我遇到的问题是ModuleModelInstance中的函数getModule()-

function getModule(name){
    if (currentModules[name]) {
        return true;
    } else {
        return undefined;
    }
}

我希望能够在另一个模块中调用它,在那里注入它,并做一些类似的事情

moduleModel.getModel(name)

并让它返回true或undefined。我在这里做错了什么?谢谢另外,-currentModules是在其正上方的范围中定义的,因此它也可以访问这里的it(只是为了澄清)。

如果我在注入它的另一个模块中console.log(moduleModule),我可以在控制台中看到完整的功能,比如-

   function ModuleModelInstance(name, callback) {
            if (currentModules[name]) {
                $log.warn(name + " module already exists.");
            } else {
                this.currentState = {};
                this.callback = callback;
                this.rootScope = $rootScope.$id;
                _.extend(currentModules, _.object([
                    [name, this]
                ]));
            }
            function getModule(name){
                if(currentModules[name]){
                    return true;
                }else{
                    return undefined;
                }
            }
        }

但是,似乎无法使用moduleModel.getModule()访问它。

按照您的要求行事-

   change
   function getModule(name){    
   to
   ModuleModelInstance.getModule = function(name){

最新更新