在angularjs控制器之间保持数据

时间:2015-01-19 16:03:19

标签: angularjs

我想将我的数据从一个控制器恢复到另一个控制器,知道我改变了我的视图,让我解释一下我有我的道路,对于我有一个控制器的每条道路,我设法将数据控制器传递给另一个但我有在正确的观点。我第一次检查我的数据通过ng-click下拉下来恢复,然后他去了另一个控制器,问题是,当我在正确的视线上它可以工作,但如果我在另一个视图我选择一个项目,我花了正确的观点,没有任何反应

   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>

任何帮助,请使用此代码,但我必须在正确的视图中

1 个答案:

答案 0 :(得分:1)

在控制器和指令之间存储和传递数据的Angular方法是使用服务。由于服务是单例,因此存储数据并可以注入指令和控制器。