Arianule Arianule - 6 months ago 21
AngularJS Question

angular.js:13424 TypeError: $scope.entry.update is not a function. Updating the correct way with angularjs

I am trying to make an update to an existing object but get the following error

$scope.entry.update is not a function
.

I created a service called 'budgetResource'

"use strict";
angular.module("common.services").factory("budgetResource", ["$resource", "appSettings", budgetResource])

function budgetResource($resource, appSettings) {

return $resource(appSettings.serverPath + "api/budget/:id", null,
{
'update': { method: 'PUT', isArray: true },
'delete': { method: 'DELETE', isArray: true },
'save': { method: 'POST', isArray: true }
});

}


Herewith the function in my controller where budgetResource service is injected with the function $scope.updateBudgetAmount being called.

$scope.updateBudgetAmount = function (categoryId) {

$scope.entry = new budgetResource();
$scope.entry = {
"budgetAmount": $scope.budgetAmount,
"categoryId": categoryId
}
$scope.entry.update({ id: categoryId },
function (data) {
$scope.categories = data;
$scope.category = "";
},
function (error) {
$scope.message = error.statusText;
});
}


which in turn calls the webapi method

public IHttpActionResult Put(int id, [FromBody]Category cat)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
BudgetRepository repo = new BudgetRepository();
var categories = repo.SaveCategory(cat);
return Ok(categories);
}


How can modify this so that it is dine correctly?

Answer

After you do $scope.entry = {...},$scope.entry becomes a plain javascript object, so $scope.entry.update is not exist.