我在我的应用程序中使用 AngularJs。我进行 http
调用并获取响应。响应包含一个数组,我将其作为 result
放入 $scope
对象中。我在某个属性上放置了一个 watch
并尝试访问放置在 $scope 对象内的存储对象。如果我打印 result
对象,我会看到它包含数组,但是当我尝试使用 length
等数组属性时,它会抛出错误。我也无法使用其他数组方法,例如 results.data[0]
。
请让我知道我哪里出错了。一些用于理解目的的代码:
var processResponse = function (result) {
$scope.results = result.data;
}
$scope.$watch('attribute', function(newVal) {
console.log($scope.results.length)
});
最佳答案
取决于你如何调用它。如果结果是一个 promise ,你可以尝试这样做:
result.then(successCallback, errorCallback);
关于javascript - 将 http 响应数组放入 $scope 后无法使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34134025/