3

I have a controller that accesses a resource Tag 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?

4

2 回答 2

5

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

于 2014-07-29T20:25:06.037 回答
1

I think what you need is this:

$scope.tags = Tag.query(function() {
    $scope.tags['active'] = true;
});

When the server finishes calling the function that adds the property is executed.

I recommend you read https://docs.angularjs.org/api/ngResource/service/$resource

于 2014-07-29T20:24:59.247 回答