AngularJS工厂的getter和setter对象

时间:2015-04-16 13:59:13

标签: javascript angularjs

我尝试为Supercontainer对象制作一个getter和setter,但它似乎不起作用。

PrometeiaECAProModuleMain.factory('ParameterFactory', [function () {


var Supercontainer = function (userId, supercontainerId, bankId) {
    this.userId = userId; 
    this.supercontainerId = supercontainerid;
    this.bankId = bankId;
};



return {

    setSupercontainerParam: function (userId, supercontainerid, bank) {
        supercontainer = new Supercontainer(userId, supercontainerid, bank);
    },

    getSupercontainerParam: function () {
        return supercontainer;
    }
   };
 }]);

2 个答案:

答案 0 :(得分:9)

我在service.js

中使用它
.factory('CommonService', function ($http, $state, Ls, md5, $filter) {
var headInfo = [];
return {
    setData: function (key, data) {
        headInfo[key] = data;
    },
    getData: function (key) {
        return headInfo[key];
    }
});

在Controller中,您可以设置这样的数据

CommonService.setData('Dataname',{name:bla, price:25});
CommonService.getData('Dataname');

因此,我可以将所有数据从一个控制器传递到另一个控制器并随处可用

答案 1 :(得分:0)

服务示例。可以使用ParameterFactory.getSupercontainer();

访问这些功能
PrometeiaECAProModuleMain.service('ParameterFactory', function () {
    var data = {};

    this.setSupercontainer = function (userId, supercontainerId, bankId) {
        data = {
            'userId': userId,
            'supercontainerId': supercontainerId,
            'bankId': bankId
        };
    }

    this.getSupercontainer = function () {
        return data;
    }
 });