JBlaze321 JBlaze321 - 2 years ago 93
Javascript Question

Pushing array of objects data via service for multiple controllers angularjs

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.

Answer Source

You can create a service for this. Like:

.service('FavoritesService', function(){
    var favorites = [];

    this.getFavorites = function(){
        return favorites;

    this.setFavorite = function(favorite){

Set your favorites:

if (!$scope.myFaveItems.some(isAlreadyPresent)) {

Use it in your ListCtrl:

.controller('ListTabCtrl', function($scope, FavoritesService) {
    $scope.myFaveItems = FavoritesService.getFavorites();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download