【问题标题】:Can not read value after angular push角推后无法读取值
【发布时间】:2016-02-09 03:24:03
【问题描述】:

我有一些如下代码:

$scope.offer = [];
angular.forEach($scope.details, function (product) {
  SomeAPI.ById.query({ product: product.id }, function (response) {
    $scope.offer.push(response);
  });
  return $scope.offer;
});
console.log($scope.offer);

为什么控制台日志仍然输出“[]”? 在页面上,{{ offer }} 正确显示值。

【问题讨论】:

标签: javascript angularjs return push


【解决方案1】:

你的 API 是异步函数

SomeAPI.ById.query({ product: product.id }, callback);  
console.log('query is still running. callback will be done later')

上述回调将在异步函数完成后调用。你的 console.log 会立即被调用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-24
    • 2021-12-08
    • 2022-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    相关资源
    最近更新 更多