Link to home
Start Free TrialLog in
Avatar of JElster
JElsterFlag for United States of America

asked on

AngularJS - Factory to hold an Array

How can I update this code to  hold an array of   values.

I want to hold FIELDNAME  and the VALUE

I want to set / add to the list by doing the following - add a value to the array/list.
       

   userFilters.setData(' lastname', 'smith');
   userFilters.setData(' firstname', 'bob');
   userFilters.setData(' Mi', 'D');

And have the object hold an Array of

'lastname','smith'
'firstname','bob'
'mi','D'





App.factory('userFilters', [function () {

    var data = {};

    var getData = function (field) {
        return data[field];
    };

    var setData = function (field, value) {
        data[field] = value;
    };

    return {
        getData: getData,
        setData: setData
    }
}]);
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Still I think this is not the right approach.
How are you planning to get that data? You still need an ID right?

userFilters.setData(1, { lastname: 'smith', firstname: 'bob', mi: 'D' });
userFilters.setData(2, { lastname: 'doe', firstname: 'john', mi: 'E' });

Open in new window


To get the last name of the user 2, you'll do:
userFilters.getData(2).lastname;

Open in new window

Avatar of JElster

ASKER

I just want to set the values and loop through the array

I want to set like this

   userFilters.setData(' lastname', 'smith');
   userFilters.setData(' firstname', 'bob');
   userFilters.setData(' Mi', 'D');
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JElster

ASKER

See my next question.. thx