alf
alf

Reputation: 681

modifying angular promises

I have a service like this (modified for brevity (and privacy)):

angular.module('services', [])
.factory('obj', function ($http) {
    return {
       get: function () {
           return $http.get('/obj').then(function (response) {
               return response.data;
           });
       }
    }
});

It's used like this: $scope.obj = obj.get(); to retrieve an object from the server.

I can display the object and modify MOST of it's keys, except I can't seem to modify any keys that contain arrays or other objects. I get a type error:

TypeError: Cannot call method 'indexOf' of undefined

I'm trying to add to the contents of an array that exists in the promised object. I can view them, it's just that when I try and add or remove them, angular thinks it's undefined.

Why is this happening?

Upvotes: 1

Views: 70

Answers (1)

tymeJV
tymeJV

Reputation: 104805

Use .then in your controller and do the logic in the callback:

obj.get().then(function(data) {
    $scope.obj = data;
    //do stuff
});

Upvotes: 3

Related Questions