0

I'm new to angular so I apologise is this is a dumb question. I have a WebAPI method that creates data. I would like to return an integer from this method that indicates the new resource's id. If I return the whole object it works in my angular controller, yet when I only return the int I get an object with a prototype and I can't seem to get the id. Fiddler shows my integer value in the TextView, so the answer is there, its just a matter of reading it with angular (javascript).

Any suggestions will be appreciated.

My service looks like this:

(function () {
    'use strict';

    var app = angular.module('app');

    app.factory('itemSvc', function ($resource) {
        return $resource("api/item",
            {},
            {
                createNew:
                    {
                        method: 'POST'
                    }           
            });
    })
})();

and I call it like this:

itemSvc.createNew($scope.newItem).$promise.then(
                function (item) {
                    var y = item;
                    $scope.items.push(item);
                },
                function (error) {})
3
  • presuming each of your items has an id property: $scope.items.push(item.id); Commented Jan 31, 2016 at 19:38
  • Thanks Johannes but I would prefer to not return the item object from WebAPI, only the integer id. the call I have there is for the item object, which works. Commented Feb 1, 2016 at 2:18
  • Possibly a dupe: stackoverflow.com/questions/22696395/… Commented Feb 1, 2016 at 3:08

1 Answer 1

0

You can create an instance of the resource, save it and obtain its id:

var newItem = new itemSvc($scope.newItem);
newItem.$save();
$scope.items.push(newItem.id);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.