我正在尝试从 Angular 范围内获取数组内的所有对象。我正在使用 jQuery 来获取数组,但我不确定如何从数组中获取每个对象,而不必像 [0] 那样定义它。
angular.element("#scopes").scope().fixtures;
这给了我:
数组[380]
然后我可以从中选择,但我需要一次性将它们全部获取。
谢谢。
最佳答案
编辑:这就是我实现此解决方案的方式。请注意,它不再使用任何 jQuery。它从 API 检索数据,然后迭代数组中的每个项目,以便您可以使用它执行您想要的操作。 angular.forEach Docs
// In your angular controller...
$http({
url: "myApiUrl",
method: "GET",
cache: false,
params: {
//whatever API params you want to pass
}
}).then(function successCallback(response) {
$scope.fixtures = response.data;
$scope.fixtures.forEach(function (element, index) {
// do what you want - as per your comments...
console.log("Element: " + index);
console.log("Status: " + element.status);
console.log("________________________________");
});
}, function failureCallback() {
alert("There was an error retrieving the data!");
});
关于javascript - 从 Angular 范围内的数组获取所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36730879/