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) {})
Adriaan Davel
  • 710
  • 1
  • 11
  • 25
  • presuming each of your items has an `id` property: `$scope.items.push(item.id);` – Johannes Jander Jan 31 '16 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. – Adriaan Davel Feb 01 '16 at 02:18
  • Possibly a dupe: http://stackoverflow.com/questions/22696395/angular-resource-does-not-parse-correctly-an-integer-response – lintmouse Feb 01 '16 at 03:08

1 Answers1

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);
gnerkus
  • 11,357
  • 6
  • 47
  • 71