简体   繁体   English

AngularJS工厂,用于在控制器之间共享数据

[英]AngularJS factory for sharing data between controllers

I have an AngularJS application (1.4.10) and I need to share some data between two controllers. 我有一个AngularJS应用程序(1.4.10),我需要在两个控制器之间共享一些数据。

So I made my factory: 所以我做了我的工厂:

.factory('CardsForService', function($http, URL){
    var service = {
        "block_id": '',
        "service_id": ''
    };

    service.save_data = function(block_id, service_id){
        service.block_id = block_id;
        service.service_id = service_id;
    };

    service.get_data = function(){
        return service;
    };

    return service;
})

I insert the data in the first controller: 我将数据插入第一个控制器:

$scope.open = function(id, type){
    console.log(id +" "+type);
    CardsForService.save_data(id, type);
    ...

And I try to get the data in another controller, like this: 我尝试在另一个控制器中获取数据,如下所示:

$scope.$on('$routeChangeSuccess', function() {
    if  (algo_to_used == "service"){
        var data = CardsForService.get_data();
        console.log(data);
    } else {
    }
});

The console.log output this: console.log输出如下:

Object {block_id: "", service_id: ""}

If I try the same get_data() function in the same controller where I call the save_data() function I have the correct results. 如果我在调用save_data()函数的同一控制器中尝试相同的get_data()函数,则结果正确。

What am I missing? 我想念什么?

Change Factory Like this 像这样改变工厂

app.factory('CardsForService', function(){
var service = {
    "block_id": '',
    "service_id": ''
};

var save_data = function(block_id, service_id){
    service.block_id = block_id;
    service.service_id = service_id;
};

var get_data = function(){
    return service;
};

return{
   saveData:save_data,
   getData:get_data
}});

And in controllers 在控制器中

app.controller('FirstCtrl',function(CardsForService){
    CardsForService.setData(id, type);
});

app.controller('SecondCtrl', function($scope, CardsForService){
    $scope.data = CardsForService.getData();
});

This sounds like it could be a timing issue. 听起来这可能是时间问题。 Data from a service like this isn't reactive. 这样的服务中的数据不是被动的。 Here's a snippet that should help visualize it. 这是一个有助于可视化的代码段。

 var app = angular.module("demo", []); app.factory("MySvc", function() { var data = {}; data.setData = function(key, value) { this[key] = value; } data.getData = function(key, def) { return key in this ? this[key] : def; }; return data; }); app.controller("test1", ["$scope", "MySvc", "$timeout", function($scope, MySvc, $timeout) { $timeout(100).then(function() { MySvc.setData("foo", "bar"); $scope.data = MySvc.getData("foo"); }); } ]); app.controller("test2", ["$scope", "MySvc", "$timeout", function($scope, MySvc, $timeout) { $timeout(500).then(function() { $scope.data = MySvc.getData("foo", "baz"); }); } ]); app.controller("test3", ["$scope", "MySvc", function($scope, MySvc) { $scope.data = MySvc.getData("foo", "asdf"); } ]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js "></script> <div ng-app="demo"> <pre ng-controller="test1">Test 1: {{ data }}</pre> <pre ng-controller="test2">Test 2: {{ data }}</pre> <pre ng-controller="test3">Test 3: {{ data }}</pre> </div> 

Ok I solved the problem. 好的,我解决了问题。 Basically before I was using this code for the redirect to the new page: 基本上,在我使用以下代码重定向到新页面之前:

$window.location.assign('/cards/service');

And now I switched to this code: 现在我切换到以下代码:

$location.path('/cards/service');

And it's working. 它正在工作。

The only thing is that when it wasn't working when I redirect the page the console in the chrome inspector refresh for every reloading, now the console do not refresh. 唯一的事情是,当我重定向页面时,如果铬笔检查器中的控制台在每次重新加载时都无法刷新,则该控制台无法正常工作。 Can someone tell me the difference between those two functions? 有人可以告诉我这两个功能之间的区别吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM