javascript - Angular JS: Store objects from one array to different arrays/services -
i started develop pretty simple app in angular js service/factory holds data i'm able push objects to.
i have array of different people (see html) can add candidates using addcandidate() function located in factory.
my problem want able store people/objects array more 1 array, e.g. candidates2 , candidates3 depending on route/controller active.
am on right track or should think different?
factory , controllers:
var app = angular.module('myapp', []); app.factory('candidates', function (){ var candidates = []; /* want fill array objects myctrl2 */ var candidates2 = []; return{ addcandidate: function(candidate){ candidates.push(candidate); } /* more functions related candidates */ } }); app.controller('myctrl1', function($scope, candidates){ $scope.addcandidate = function(candidate){ candidates.addcandidate(candidate); } }); /* want push candidates candidates2 here */ app.controller('myctrl2', function($scope, candidates){ $scope.addcandidate = function(candidate2){ candidates.addcandidate(candidate2); } }); /* more controllers of same kind */ html:
<ul> <li ng-repeat="person in people"> <h2>{{ person.name }}</h2> <button ng-click="addcandidate(person)">add candidate</button> </li> </ul>
it looks want candidates different each controller. in case, make factory build object constructor (similar class, not really) rather giving same object every time.
see plunker example. notice each controller requests , news it's own candidate, rather using shared candidate definition.
Comments
Post a Comment