javascript - Pushing array of objects data via service for multiple controllers angularjs -
new angularjs , trying figure out how pushing array of objects data (not input strings) between controllers. currently, code pushes data 1 controller('choosetabctrl') want push controller ('listtabctrl') list displays on page. i'm confused b/c examples show when user enters string of text. project adds fave clicking button. appreciated.
you can create service this. like:
.service('favoritesservice', function(){ var favorites = []; this.getfavorites = function(){ return favorites; }; this.setfavorite = function(favorite){ favorites.push(favorite); }; });
set favorites:
... if (!$scope.myfaveitems.some(isalreadypresent)) { $scope.myfaveitems.unshift(item); favoritesservice.setfavorite(item); } ...
use in listctrl:
.controller('listtabctrl', function($scope, favoritesservice) { $scope.myfaveitems = favoritesservice.getfavorites(); });
Comments
Post a Comment