Mahmoud
Mahmoud

Reputation: 217

persist Data Between angularjs Controllers

I want to recover my data from one controller to another, knowing that I change my view, let me explain I have my roads, for each road I have a controller, I managed to passed the data controller to another but I have to be on the right view. my first check my data recovered via ng-click on a dropdown and he goes to the other controller, the problem that when I'm on the right sight it works but if I'm on another view I select an item and I spend on the right view, nothing happens

   var routeAppControllers = angular.module('routeAppControllers', []);

   // Sharing Data of venues Between Controllers
   routeApp.factory('mySharedService', function ($rootScope) {
    var sharedService = {};
    sharedService.venues = '';
    sharedService.prepForBroadcast = function(venues){
        this.venues = venues;
        this.broadcastItem();
    };
    sharedService.broadcastItem = function(){
        $rootScope.$broadcast('handleBroadcast');
    };
  return sharedService;
});

routeAppControllers.controller('GlobalController', ['$scope','$http','mySharedService', function GlobalController($scope, $http, mySharedService)
{
     var venuesName = [];
    $http.get('/ajax/venuesNameClient').success(function(data){
        angular.forEach(data, function(value,key){
            venuesName.push({name : value});
        });
        $scope.venuesName = venuesName
    });

    $scope.venueName = {};
    var venues = [];
    $scope.go = function(name) {

        $http.get('/ajax/venuesClient').success(function(data){
            angular.forEach(data, function(value,key){
                if (value != "ok") {
                    for (var i=0;i<value.name.length;i=i+1)
                    {
                        if (value['name'][i] == name)
                        {
                            venues = [];
                            venues.push(value['name'][i]);
                            venues.push(value['email'][i]);
                            venues.push(value['address'][i]);
                            venues.push(value['long_description'][i]);
                            venues.push(value['creation_date'][i]);
                            venues.push(value['wifi_network'][i]);
                        }
                    }
                };
            });
            mySharedService.prepForBroadcast(venues);
        });
    }
    $scope.$on('handleBroadcast', function() {
        $scope.venues = mySharedService.venues;
    });
}
]);

// WelcomeController
routeAppControllers.controller('WelcomeController', ['$scope','mySharedService', function WelcomeController($scope, mySharedService)
{
    $scope.$on('handleBroadcast', function() {
        $scope.venues = mySharedService.venues;
        console.log($scope.venues);
    });
}
]);

<ui-select ng-model="venueName.selected" theme="select2" ng-disabled="disabled" style="min-width: 300px; padding-top:10px;">
                    <ui-select-match placeholder="Select a venue in the list ...">{{$select.selected.name}}</ui-select-match>
                    <ui-select-choices repeat="venueName in venuesName | propsFilter: {name: $select.search}">
                        <div ng-click='go(venueName.name)'>{{venueName.name}}</div>
                    </ui-select-choices>
                  </ui-select>

any help please, this code work but i have to be in the right view

Upvotes: 0

Views: 384

Answers (1)

Enzey
Enzey

Reputation: 5254

The Angular way to store and pass data between controllers and directives is to use services. As the services are singletons the store the data and can be injected into both directives and controllers.

Upvotes: 1

Related Questions