Angular的ngResource's $save()不起作用

Angular ngResource's $save() not working

本文关键字:save 不起作用 ngResource Angular      更新时间:2023-09-26

我想发布数据到我的asp.net webapi控制器,通过使用$save()方法属于ngResource我得到这个错误:" TypeError: scope.product美元。$save不是n.$scope的函数。saveProduct "

当我使用$http()时,数据被保存,但$save()给我错误,其他方法如$query()和$get()工作正常只有$save()导致错误

代码:

// first file (module)
var app = angular.module('commonServices', ['ngResource'])
                 .constant('appSettings', {
                     serverPath: 'http://localhost:29904/'
                 });
//second file (factory)
(function(){
    angular.module('commonServices')
    .factory('productResource', ['$resource', 'appSettings', productResource])
    function productResource($resource, appSettings) {
        return $resource(appSettings.serverPath + "api/Products/:id",
              null,
            {
                'update': { method: 'PUT' },
            });
    }
}());
// third file (controller)
myApp.controller('editProductController', ['$scope', '$routeParams', '$http', 'productResource',
    function ($scope, $routeParams, $http, productResource) {
        $scope.num = $routeParams.id;
        $scope.alertUser = false;

        $scope.saveProduct = function () {

            $scope.product.$save(function(data){});
                 }
        };
    }]);

//模板中的一些标记

<div class="form-group ">
                            <label class="col-md-2 control-label"
                                   for="inputProductName">Product Name</label>
                            <div class="col-md-4">
                                <input class="form-control"
                                       id="inputProductName"
                                       name="inputProductName"
                                       type="text"
                                       placeholder="Product Name (required)"
                                       required
                                       ng-model="product.productName" />
                            </div>

                        </div>
                        <div class="form-group">
                            <label class="col-md-2 control-label" for="inputProductCode">Product Code</label>
                            <div class="col-md-4">
                                <input class="form-control"
                                       id="inputProductCode"
                                       name="inputProductCode"
                                       type="text" ng-model="product.productCode">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-2 control-label"
                                   for="inputAvailabilityDate">Availability</label>
                            <div class="col-md-4">
                                <div class="form-control">
                                    {{product.releaseDate}}
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-2 control-label"
                                   for="inputDescription">Description</label>
                            <div class="col-md-4">
                                <textarea class="form-control"
                                          id="inputDescription"
                                          name="inputDescription"
                                          placeholder="Description"
                                          rows="3" ng-model="product.description"></textarea>
                            </div>
                            <br />
                        </div>
                        <div class="form-group">
                            <div class="col-md-4 col-md-offset-2">
                                <span>
                                    <button class="btn btn-primary"
                                            style="width:80px;margin-right:10px" ng-click="saveProduct()">
                                        Save
                                    </button>
                                </span>

使用$save()而不调用get我做的是:

productResource.save($scope.product, function(data) {
                });

谢谢@TzachOvadia给我提示:)

试试这个:

$scope.product = productResource.get({ id: $scope.num });
$scope.saveProduct = function () {
    $scope.product.$save(function (response) {...});
}