简体   繁体   English

在AngularJS中的GET请求后,在2个控制器之间共享全局变量失败

[英]Sharing a global variable between 2 controllers FAILED after a GET request in AngularJS

I'm using 2 different controllers sharing a global value currentOpenningFolder. 我正在使用2个共享全局值currentOpenningFolder的控制器。 I want that 2 controllers can both sync to that variable no matter what. 我希望2个控制器无论如何都可以同步到该变量。 But in this example, in the GET request I change the variable in the first controller but I can't view that variable in the second controller. 但是在此示例中,在GET请求中,我在第一个控制器中更改了变量,但无法在第二个控制器中查看该变量。 Instead it shows the initial value of currentOpenningFolder . 而是显示currentOpenningFolder的初始值。 Any ideas why this may be happening? 任何想法为什么会发生这种情况?

      angular.module('scopeExample', [])   
.controller('MyController', ['$scope','currentOpenningFolder','$http', function($scope,currentOpenningFolder,$http) {
            $scope.sayHello = function() {

          $http.get('https://api.github.com/users/chuson1996').then(function(obj){
            currentOpenningFolder = obj;
            console.log(currentOpenningFolder);
          })
        };
      }])
      .controller('MyController2', ['$scope','currentOpenningFolder', function($scope,currentOpenningFolder) {
        $scope.sayHello = function() {
          $scope.currentOpenningFolder = currentOpenningFolder;
          console.log($scope.currentOpenningFolder);
        };
      }])
      .value('currentOpenningFolder',{});

Don't assign an object to other object like this: 不要像这样将对象分配给其他对象:

currentOpenningFolder = obj;

if you assign as this, currentOpenningFolder will loose reference to original object and refer to other object obj . 如果以此方式分配,则currentOpenningFolder将失去对原始对象的引用,而对其他对象obj引用。 so best way to do this is to create a property of currentOpenningFolder and assign object to that prperty like this : 因此,执行此操作的最佳方法是创建currentOpenningFolder的属性并将对象分配给该属性,如下所示:

currentOpenningFolder.prop = obj;

and then access this to other controller like: 然后将其访问其他控制器,例如:

$scope.currentOpenningFolder = currentOpenningFolder.prop;

Edited: 编辑:

Try this plunkr 试试这个笨蛋

A very simple fix is to extend the existing object instead of replacing it. 一个非常简单的解决方法是扩展现有对象而不是替换它。 Replacing it is causing references to be broken 替换它会导致引用被破坏

Change 更改

currentOpenningFolder = obj;

To

angular.extend(currentOpenningFolder , obj);

This leaves the existing object intact ( and the other references) and merges changes into it. 这样会使现有对象(和其他引用)保持不变,并将更改合并到其中。

Also in the controllers I'm not sure why you set the scope variable that points to currentOpenningFolder within a function. 同样在控制器中,我不确定为什么要在函数中设置指向currentOpenningFolder的作用域变量。 You likely want to declare it outside the function 您可能想在函数外声明它

Without seeing more code it seems like $scope.sayHello=function(... should be replaced with $scope.currentOpenningFolder = currentOpenningFolder; 没有看到更多代码,似乎$scope.sayHello=function(...应该替换为$scope.currentOpenningFolder = currentOpenningFolder;

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

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