简体   繁体   中英

Get all objects from Array in Angular Scope

I am trying to get all objects within an array from a scope in angular. I am using jQuery to get the array but I'm not sure how to get each object from the array without having to define it like [0].

angular.element("#scopes").scope().fixtures;

This gives me:

Array[380]

Which I can then select from but I need to get them all in one go.

Thanks.

EDIT: This is how I would implement this solution. Note that it no longer uses any jQuery. It retrieves the data from the API, and then iterates over each item in the array allowing you to do what you want with it. 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!");
});

To answer your specific question, it sounds like you want to map over the results:

var statuses = angular.element("#scopes").scope().fixtures.map(function(fixture) {
    return fixture.status;
});

However, it feels like you should be able to get this data from your model instead of trying to pull it out of your view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM