Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何引用Angular服务返回的数组,以维护状态?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何引用Angular服务返回的数组,以维护状态?

Javascript 如何引用Angular服务返回的数组,以维护状态?,javascript,angularjs,Javascript,Angularjs,我有一个名为“categories”的主对象,它由数据服务返回,并由许多不同的控制器调用 .service('DataService', ['$http', '$q', '$timeout', function($http, $q, $timeout) { var categories = {}; // Return public API. return({ setCategory: setCategory, getCategory:

我有一个名为“categories”的主对象,它由
数据服务返回,并由许多不同的控制器调用

.service('DataService', ['$http', '$q', '$timeout', function($http, $q, $timeout) {

    var categories = {};

    // Return public API.
    return({
        setCategory: setCategory,
        getCategory: getCategory
    });

    function setCategory(activities, details) {
        var name = details.shortName,
            category = {};
        category.activities = activities;
        category.details = details;
        categories[name] = category;
    }

    function getCategory(name) {
        return categories[name];
    }
为了保持所有控制器之间的状态,从控制器访问
类别
的正确方法是什么

例如,在Controller1中,我想获取与
名称
匹配的
类别
对象属性,然后向其中包含的活动添加新值:

$scope.category = getCategory(name);
$scope.category.activities[2].input = true;
然后,我希望能够访问
category.activities[2]的值。在使用
getCategories
服务的任何地方输入
。但据我所知,这不起作用,因为我已将
类别[name]
重新分配给
$scope.category
,对吗


或者,我错了,或者采取了完全错误的方法?从
类别
对象调用对象的最佳方式是什么,以允许我在所有控制器上保留状态?

请不要使用
$broadcast
。使用
$watch

.value('categories',{})
.service("myService",function(categories,$rootScope) {
   $rootScope.$watch(function(){
      return categories;
   },function(val){
      console.log("Some controller, any controller, has updated categories!");
   },true);
})

建议您在每次控制器进行更改时调用DataService.setCategory。然后,您可以使用$broadcast发送服务已更改的消息,并使用$on订阅并触发“getCategory”方法的刷新,以便在每个控制器中都有最新的模型

.service('DataService', ['$http', '$q', '$timeout', function($http, $q, $timeout) {

    var categories = {};

    // Return public API.
    return({
        setCategory: setCategory,
        getCategory: getCategory
    });

    function setCategory(activities, details) {
        var name = details.shortName,
            category = {};
        category.activities = activities;
        category.details = details;
        categories[name] = category;
    }

    function getCategory(name) {
        return categories[name];
    }
例如,在您的服务中:

.service('DataService', ['$rootScope', '$http', '$q', '$timeout', function($rootScope, $http, $q, $timeout) {

    function setCategory(activities, details) {
        var name = details.shortName,
        category = {};
        category.activities = activities;
        category.details = details;
        categories[name] = category;

        $rootScope.$broadcast('categories:updated');
    }
在控制器中:

.controller('ControllerOne', ['$scope', '$rootScope', 'DataService', function($scope, $rootScope, DataService) {

    $scope.category = DataService.getService(name);

    $scope.category.activities[2].input = true;

    DataService.setCategory(category.activities, category.details);

}])

.controller('ControllerTwo', ['$scope', '$rootScope', 'DataService', function($scope, $rootScope, DataService) {

     $scope.category = DataService.getCategory(name);

     $scope.$on('categories:updated', function(){
         $scope.category = DataService.getCategory(name)             
     });

}]);

只要确保每次控制器进行更改时都使用DataService.setCategory。然后,您可以使用$broadcast发送服务已更改的消息,并使用$on触发“getCategory”方法的刷新,以便在每个控制器中都有最新的模型。