[英]Get all objects from Array in Angular Scope
我试图从角度范围获取数组内的所有对象。 我正在使用jQuery来获取数组,但是我不确定如何从数组中获取每个对象而不必像[0]那样对其进行定义。
angular.element("#scopes").scope().fixtures;
这给了我:
Array[380]
然后可以从中进行选择,但我需要一次性完成所有操作。
谢谢。
编辑:这就是我将如何实现此解决方案。 请注意,它不再使用任何jQuery。 它从API检索数据,然后遍历数组中的每个项目,从而使您可以执行所需的操作。 angular.forEach文档
// 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!");
});
要回答您的特定问题,听起来您想映射结果:
var statuses = angular.element("#scopes").scope().fixtures.map(function(fixture) {
return fixture.status;
});
但是,感觉您应该能够从模型中获取此数据,而不是尝试将其从视图中拉出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.