FrankTan
FrankTan

Reputation: 1686

AngularJS factory getter and setter object

i try to make a getter and setter for the Supercontainer object, but it dose not seems to work.

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;
    }
   };
 }]);

Upvotes: 2

Views: 8568

Answers (2)

Service example. Can access the functions with 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;
    }
 });

Upvotes: 0

stackg91
stackg91

Reputation: 594

I use it like this in my 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];
    }
});

In the Controller you would set ur data like this

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

So I can pass all my data from one Controller to another and have it available everywhere

Upvotes: 8

Related Questions