kontur kontur - 2 months ago 11x
AngularJS Question

Iterate over returned angular resource in controller

I have a controller that accesses a resource

like so:

$scope.tags = Tag.query();

which resolves to something like this:

$scope.tags = [
{ name: "tag1", label: "Tag1" },
{ name: "tag2", label: "Tag2" },
{ name: "tag3", label: "Tag3" },
{ name: "tag4", label: "Tag4" },

For this particular controller, the returned tags should have an additional attribute
"active": true
, like
{ name: "tag1", label: "Tag1", active: true }

How can I iterate over the returned promise once it is resolved to add this boolean?


Use the promise.then() function.

Tag.query().$promise.then(function (results) {
    angular.forEach(results, function (result) {
        result.active = true;

    $scope.tags = results

see the docs on $q