0

I want to post data to my asp.net webapi controller, by using $save() method that belong to ngResource i am getting this error: "TypeError: $scope.product.$save is not a function at n.$scope.saveProduct "

when i used $http(), data is getting saved but $save() is giving me error, other methods like $query() and $get() are working properly only $save() is causing an error.

code:

// 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){});
                 }
        };
    }]);

// some markup from template

<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>

4

2 回答 2

2

使用 $save() 而不调用 get 我所做的是在这里:

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

                });

感谢@TzachOvadia 为我提供线索:)

于 2015-08-04T19:55:04.720 回答
1

尝试这个:

$scope.product = productResource.get({ id: $scope.num });

$scope.saveProduct = function () {
    $scope.product.$save(function (response) {...});
}
于 2015-08-04T18:45:59.243 回答