Reputation: 93
new to AngularJS and trying to figure out how to pushing an array of objects data (not input strings) between controllers. Currently, my code pushes data into one controller('ChooseTabCtrl') but I want to push to another controller ('ListTabCtrl') so that the list displays on another page. I'm confused b/c most examples show only when a user enters a string of text. My project adds a fave by clicking a button. Any help would be appreciated.
Upvotes: 1
Views: 706
Reputation: 1040
You can create a service for this. Like:
.service('FavoritesService', function(){
var favorites = [];
this.getFavorites = function(){
return favorites;
};
this.setFavorite = function(favorite){
favorites.push(favorite);
};
});
Set your favorites:
...
if (!$scope.myFaveItems.some(isAlreadyPresent)) {
$scope.myFaveItems.unshift(item);
FavoritesService.setFavorite(item);
}
...
Use it in your ListCtrl:
.controller('ListTabCtrl', function($scope, FavoritesService) {
$scope.myFaveItems = FavoritesService.getFavorites();
});
Upvotes: 1