Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.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 AngularJS-工厂变量不';控制器中的t更新_Javascript_Angularjs_Angularjs Scope_Angularjs Controller_Angularjs Factory - Fatal编程技术网

Javascript AngularJS-工厂变量不';控制器中的t更新

Javascript AngularJS-工厂变量不';控制器中的t更新,javascript,angularjs,angularjs-scope,angularjs-controller,angularjs-factory,Javascript,Angularjs,Angularjs Scope,Angularjs Controller,Angularjs Factory,我和AngularJS面临一个新问题。 事实上,因为我需要在两个控制器中有一个“共享”变量,可读且可更新,所以我考虑在两个控制器中都注入一个工厂。数据是通过http请求加载的,但一旦请求完成,var就不会更新。这是我的密码: //Common factory app.factory('CurrentGallery',['$q','$http',function($q,$http){ var data = null; //HTTP request to API v

我和AngularJS面临一个新问题。 事实上,因为我需要在两个控制器中有一个“共享”变量,可读且可更新,所以我考虑在两个控制器中都注入一个工厂。数据是通过http请求加载的,但一旦请求完成,var就不会更新。这是我的密码:

  //Common factory
  app.factory('CurrentGallery',['$q','$http',function($q,$http){
    var data = null;

    //HTTP request to API
    var loadImages = function(query){
      $http.post("https://mywebsite.com/api/?"+query).then(function(result){
        update(result.data);
      });
    }

    //Update the data var
    var update = function(data){
      data = data;
      console.log("Update called", data);
    }

    return {
      loadImages: loadImages,
      update: update,
      data: data
    }
  }]);

  //The controller
  app.controller("PhotoBestController", ['$scope', 'CurrentGallery', function ($scope,CurrentGallery) {
    //$scope.pics is basically the variable I want to update inside my controller
    $scope.pics = CurrentGallery.data;

    //Send the data of the query for the API
    CurrentGallery.loadImages("userInfo=true&exifInfo=true&order=tot_like,DESC");

    $scope.$watch(CurrentGallery, function(){
      console.log("CurrentGallery has changed");
    });
  }]);
这是我在控制台中获得的日志:

  • CurrentGallery已更改
  • 已调用更新,对象{…}
因此,当CurrentGallery为null时,它似乎第一次得到更新,但是,即使它在工厂内得到更新,它也不会更新$scope.pics变量


有什么建议吗?

更新
我遵循了此线程中的代码逻辑:


更新
我遵循了此线程中的代码逻辑:


我认为您的数据仅在工厂更新。因此,要在控制器中更新它,您必须从工厂再次获得它

因此,在控制器中放置手表的位置重新分配范围变量:

$scope.$watch(CurrentGallery, function(){
      $scope.pics = CurrentGallery.data;
      console.log("CurrentGallery has changed");
});

我认为您的数据仅在工厂更新。因此,要在控制器中更新它,您必须从工厂再次获得它

因此,在控制器中放置手表的位置重新分配范围变量:

$scope.$watch(CurrentGallery, function(){
      $scope.pics = CurrentGallery.data;
      console.log("CurrentGallery has changed");
});

这可能会帮助你@VinodLouis它似乎并没有解决问题!它给出了完全相同的结果,因此没有任何更新这可能会帮助您@VinodLouis它似乎无法解决问题!它给出了完全相同的结果,因此没有任何更新是的,我考虑过,但如果我使用承诺,那么我如何检测到与我共享CurrentGallery.data var的潜在第二个控制器中的更改?是的,我考虑过,但如果我使用承诺,那么,我如何才能检测到与我共享CurrentGallery.data变量的潜在第二个控制器中的更改?这就是我所做的,但它甚至没有触发日志“CurrentGallery has change”(CurrentGallery has change),这不是强制性的。您发布的代码中有此日志,因此我将其保存在这里。它是否有效。我就是这么做的,但它甚至没有触发日志“CurrentGallery已更改”,这不是强制性的。这是您发布的代码中的日志,所以我将其保留在这里。那么它是否有效呢。