【问题标题】:How to update $scope variable with 'http get'?如何使用“http get”更新 $scope 变量?
【发布时间】:2017-03-23 23:40:04
【问题描述】:

努力理解 http 请求和承诺的概念。我知道 http 请求是异步的,但不知道如何实现承诺并返回数据以更新 $scope 变量,以便在任何地方都可以访问。

此时 $scope 变量只在 '.then' 内打印出来

基本上我正在尝试将“season_number”返回到一个数组中,以便其显示为:

Array[

{season_number: 1},
{season_number: 2},
{season_number: 3},
{season_number: 4},
{season_number: 5}

];

代码:

$scope.seasonDetails = [];

var getSeasons = function() {
var data = "";
 $http({
    method: 'GET',
    url: url

  })

  .then(function(response) {
    var tvSeasonDetails = [];
    for (var i = 0; i < response.data.seasons.length; i++) {
      tvSeasonDetails.push(response.data.seasons[i].season_number)
    }

    $scope.seasonDetails = tvSeasonDetails;
    console.log($scope.seasonDetails); //<----Variable displayed here

  })
}

  getSeasons();
  console.log($scope.seasonDetails); //<---- Empty Array displayed here

【问题讨论】:

标签: javascript angularjs http asynchronous scope


【解决方案1】:

正如您所指出的,$http.get() 是异步的。所以当你调用getSeasons时,它不会马上设置$scope.seasonDetails的值,而是在收到响应后才设置。

所以,是的,在您致电 console.log 之后,它不会在您致电 getSeasons 后立即可用,但一旦可用,它最终会到达那里。

如果您只需要数据在某个时间点进入范围(因此可以通过ng-repeat 或类似方式将其显示在您的页面中),那么您无需再做任何事情。 Angular 将在更改到达时应用更改。

如果您确实需要处理结果,那么您可以直接在现有的then 中进行操作,或者让getSeasons 返回$http 承诺,并在调用getSeasons 之后添加一个新的 then .哪个更相关取决于您的实际需求(例如,从多个地方调用 getSeasons,但您有不同的后处理,具体取决于调用它的位置)。

选项 1:

var getSeasons = function() {
var data = "";
 $http({
    method: 'GET',
    url: url

  })

  .then(function(response) {
    var tvSeasonDetails = [];
    for (var i = 0; i < response.data.seasons.length; i++) {
      tvSeasonDetails.push(response.data.seasons[i].season_number)
    }

    $scope.seasonDetails = tvSeasonDetails;
    console.log($scope.seasonDetails); //<----Variable displayed here

    ... add your call to any function that needs access to $scope.seasonDetails here...
  })
}

选项 2:

var getSeasons = function() {
var data = "";
 // the only change is right here
 return $http({
    method: 'GET',
    url: url
  })

  .then(function(response) {
    var tvSeasonDetails = [];
    for (var i = 0; i < response.data.seasons.length; i++) {
      tvSeasonDetails.push(response.data.seasons[i].season_number)
    }

    $scope.seasonDetails = tvSeasonDetails;
    console.log($scope.seasonDetails); //<----Variable displayed here

  })
}

getSeasons().then(function()
{
   ... your call to whatever function needs $scope.seasonDetails here ...
});

【讨论】:

  • 我需要在此之后直接在函数中使用'$scope.seasonDetails',但总是给出一个空数组
  • 然后将调用移至现有then 中的该函数,或者让getSeasons 返回一个Promise(即return $http.get(...)),然后在then 中调用该函数(即getSeasons().then(function() { youcallhere }) 或将承诺传递给此函数,以便它可以自己执行then。
  • 编辑后的答案有两个选项。
【解决方案2】:

如果seasonDetails发生变化后需要对一些结果进行操作,可以使用$scope.$watch(...)函数

 $scope.$watch('seasonDetails', function(newValue, oldValue) {
      console.log($scope.seasonDetails);
 });

【讨论】:

  • $watch 不会比 Promise 上的 then 增加更多开销吗?
  • @jcaron 我同意你的观点,没有必要用 $scope.$watch 污染你的代码,除非它是 Promises 无法实现你需要的最后手段。
猜你喜欢
  • 2017-07-20
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多