初始化角度工厂时如何传递参数

时间:2014-02-26 14:35:50

标签: javascript angularjs

假设我有一个包含大量原始数据的角度工厂(我们称之为_fakeData),我想根据初始化期间传递的参数公开部分数据(让我们称之为_exposedData)

我现在的方法是创建一个init(语言)方法,根据语言参数的值将this._exposedData设置为某个值,并且工厂中的所有其他方法都将关闭_exposedData。

但是,为了实现这一点,我需要先调用init()方法,然后我需要检查何时为工厂的所有其他方法调用init(),这看起来非常麻烦。

我很确定我目前的解决方案非常笨重,并且想知道是否有人可以建议我更好地组建我的工厂?

angular.module('testApp', [])
.factory('TestFactory', function() {
    return  {
        //raw data set that should not be accessed or modified
        //access should be done through _exposedData instead
        _fakeData: {
            javascript: 'blah blah',
            ruby: 'blah blah blah',
            python: 'blah blah blah blah'
        },
        _isInitialised: false,
        init: function(language) {
            if (this._fakeData[language]) {
                this._exposedData = this._fakeData[language]; 
                this._isInitialised = true;
                return this;
            }
            throw new Error('Cannot initalise');
        },
        getData: function() {
            this._checkInitialised();
            return this._exposedData;
        },
        //checks whether init() has been called
        _checkInitialised: function() {
            if (!this._isInitialised) {
                throw new Error('Not initialised');
            }
        }

    }
})
.controller('TestCtrl', function($scope, TestFactory) {
    console.log(TestFactory.init('javascript').getData());
})

http://jsfiddle.net/3jJeE/

3 个答案:

答案 0 :(得分:3)

您可以尝试使用module.run方法。在run

中调用init方法
myappModule.run(function(TestFactory) {
   TestFactory.init('javascript');
});

答案 1 :(得分:1)

angular.module("TestApp",[]).provider("TestData", function() {
    var _fakeData =  {
        javascript: 'blah blah',
        ruby: 'blah blah blah',
        python: 'blah blah blah blah'
    };
    this._exposedData = {};
    this.setLanguage = function(x) {
      this._exposedData = _fakeData[x];
    }
    var _me = this;
    this.$get = function () { //this method should return what factory does
        return {
           ....
           getData: function () {
               return _me.exposedData;
        }
   }
});
angular.module("TestApp").config(function(TestDataProvider)
    TestDataProvider.setLanguage('javascript');
);

希望我至少部分理解你的问题...... ; - )

答案 2 :(得分:0)

首先,您需要在_fakeData块之外移动return对象,否则您的控制器只能访问它。

var _fakeData = {
        javascript: 'blah blah',
        ruby: 'blah blah blah',
        python: 'blah blah blah blah'
};
return {
...
};

其次,我会使用moduleconstant方法来定义语言:

var app = angular.module('testApp', []);
app.constant('language', 'javascript');

然后在您的工厂注入language,这变得更加简单:

app.factory('TestFactory', ['language', function(language) {
   var _fakeData = {
        javascript: 'blah blah',
        ruby: 'blah blah blah',
        python: 'blah blah blah blah'
   };
   return {
      getData: function() {
        return _fakeData[language];
      }
   };
}];

不再需要init()方法。只需在控制器中使用getData():

app.controller('TestCtrl', function($scope, TestFactory) {
   console.log(TestFactory.getData());
});
相关问题