【问题标题】:Angular ngResource's $save() not workingAngular ngResource 的 $save() 不起作用
【发布时间】:2015-10-27 07:03:19
【问题描述】:

我想通过使用属于 ngResource 的 $save() 方法将数据发布到我的 asp.net webapi 控制器我收到此错误: "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>

【问题讨论】:

标签: javascript angularjs save ngresource angularjs-ng-resource


【解决方案1】:

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

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

                });

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

【讨论】:

  • 我太专注于 $save... 应该退后一步。
【解决方案2】:

试试这个:

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

$scope.saveProduct = function () {
    $scope.product.$save(function (response) {...});
}

【讨论】:

  • 这个对象是通过 ng-model 从 html 内的字段中填充的
  • 我很容易更新数据,但唯一给我错误的是 $save()
  • 您可以添加您的模板以及您的 $query 和 $get 调用吗?
  • productResource.get({ id: $scope.num }, function (data) { $scope.product = data;} 我在同一个控制器上使用 thia get
  • 感谢它现在正在工作的答案,但是如果我不想要 get() 而只需要在控制器上使用 $save() 怎么办...
猜你喜欢
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-29
相关资源
最近更新 更多