angular.js:13424 TypeError:$ scope.entry.update不是函数。使用angularjs更新正确的方法

时间:2016-05-25 07:00:05

标签: angularjs asp.net-web-api

我正在尝试对现有对象进行更新,但会收到以下错误$scope.entry.update is not a function

我创建了一个名为'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 }
        });       

}

这里是我的控制器中的函数,其中使用函数$ scope.updateBudgetAmount注入budgetResource服务。

$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;
        });
    }

反过来调用webapi方法

 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);
        }

如何修改它以便正确用餐?

1 个答案:

答案 0 :(得分:1)

执行$scope.entry = {...}后,$scope.entry成为普通的javascript对象,因此$scope.entry.update不存在。