从承诺返回后更新作用域变量

Angular Updating scope variables after returning from a promise

本文关键字:作用域 变量 更新 承诺 返回      更新时间:2023-09-26

我在我的一个控制器中使用q服务来确保我的请求在绑定then子句中的响应之前完成。现在是棘手的部分。页面上有一个指令,它的模板更新了一个作用域变量。这个作用域变量用于在响应json的不同部分之间切换,如果你愿意,也可以是一个选择器。我需要在加载页面后更新then子句中的变量集。它由指令中添加的id来设置。我似乎找不到一个有效的方法来更新它们。

$scope.selector = {}; //property added from a child scope
$q.all({
     //some factory calls and assignment to properties
}).then(function(responses){
  //scope variable assignments off of the responses object.
  //some assignment that uses the selector. a[selector.id] ex.
  }, function(err){
    $log.error(err);
   }).finally(function(){
      //some setting of load params
    });
  //Then I need to update those variables set in the then based on whether or not the selector id was changed in the directive template. 

在这里猜测一下,因为问题不清楚,但从它的外观来看,您应该将整个响应集保存在作用域上,然后取出所需的数据。我不明白为什么每次你想拉出一个方面时,你都要试图更新整个响应。

$scope.selector = {}; //property added from a child scope
$scope.responses = {};
$q.all({
   //some factory calls and assignment to properties
}).then(function(responses){
   //scope variable assignments off of the responses object.
   //some assignment that uses the selector. a[selector.id] ex.
   $scope.responses = responses;
}, function(err){
   $log.error(err);
}).finally(function(){
  //some setting of load params
});
// Use something like $watch here and have it call a function
AngularJS中的

观察者可能也很有帮助。