Ørnulf Arntsen
Ørnulf Arntsen

Reputation: 193

Using service, ngResource save

I'm new to Angular.
I have this Service(?) for my RESTful services.

    .factory('LanguagesService', function ($resource) {
   return $resource('http://127.0.0.1:8000/language/:langId', {
       langId: '@id'
   });
});

Then in my controller I do like this

adminLang.addLanguage = function () {
        LanguagesService.save({
            code: adminLang.newCode,
            name: adminLang.newName
        });
    }

My question is, how do I know if the save() is successful? So I can do this or that depending on if it fails or succeeds?

Thank you a lot.

Upvotes: 0

Views: 183

Answers (2)

mudin
mudin

Reputation: 2862

When you request add two callback functions as below:

LanguagesService.save({
            code: adminLang.newCode,
            name: adminLang.newName
        }, function(response){
           // success
        }, function(error){
           // error
        });

Check this for more information: http://fdietz.github.io/recipes-with-angular-js/consuming-external-services/consuming-restful-apis.html

Upvotes: 1

Pankaj Parkar
Pankaj Parkar

Reputation: 136194

$resource methods returns a promise object via $promise object, you could keep eye on that promise by placing .then.

Code

LanguagesService.save({
    code: adminLang.newCode,
    name: adminLang.newName
}).$promise.then(function(data){
   console.log(data);
   //do other awesome things
}, function(err){
});

Upvotes: 1

Related Questions