【问题标题】:Cannot blank out a field when using AngularJS and ng-resource使用 AngularJS 和 ng-resource 时无法清空字段
【发布时间】:2014-08-31 03:38:05
【问题描述】:

由于某种原因,当我更新表单并尝试清空该字段(在此示例中为“描述”)时,传递到服务器的值是原始值。

例如,如果description 的值为“description here”,然后我将表单上的值清空,则客户端显示description 字段的未定义。在 $update (ng-resource) 之后,服务器端的值是原始值。

// when it gets to the server side, foo.description is filled in with the original value 
// after trying to blank it out
foo.$update(function() {
                $location.path('foo/' + $scope.foo._id);
            }, function(errorResponse) {
                $scope.error = errorResponse.data.message;
            });

客户端的Here is the plunkr

【问题讨论】:

    标签: angularjs ngresource


    【解决方案1】:

    “未定义”的出现是因为您的输入字段中存在“必填”字段

        <input type="text" data-ng-model="foo.description" id="description" 
        class="form-control" placeholder="Description" required>
    

    如果您删除“必填”字段,$scope.foo.description 将具有空白值而不是“未定义”。

        <input type="text" data-ng-model="foo.description" id="description" 
        class="form-control" placeholder="Description">
    

    您能否尝试删除“必需”并查看您的问题是否已解决。

    【讨论】:

    • 谢谢!就是这样。
    猜你喜欢
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多