Skip to content Skip to sidebar Skip to footer

NgResource Retrive Unique ID From POST Response After $save()

So I have a Resource defined as follows: angular.module('questionService', ['ngResource']) .factory('QuestionService', function($http, $resource) { var QuestionService = $resou

Solution 1:

You need to have a success handler to assign the new id to newQ manually:

QuestionService.save(newQ, function(data) {
    newQ.id = data.id;
});

But there is a better to achieve the same. Because your QuestionService is a resource class object, you can instantiate your newQ like this:

var newQ = new QuestionService({text: $scope.addTxt});

Then, call $save() of newQ:

newQ.$save();

Now newQ should have the new id returned by the server.


Solution 2:

I have Asp.Net server with WebApi2 running, i use Ok(number) to return content, and it return for example '6' as result. once it return, the angular show an object containing promise and state, and prototypes and a deep repeated hierarchy, but no value, no '6'. So i went to see where the data goes, for seeing where the data is i define a transform, and i see awo the data, but it's not a json...

later i change it to following, and now i have a obj in my success function, which has sub property called 'returnValue' (as i defined), and this object hold my value.

transformResponse: function(data, header){
    var obj={};
    obj.returnValue=angular.fromJson(data);
    return obj;
},

Post a Comment for "NgResource Retrive Unique ID From POST Response After $save()"