laravel 为什么 angular 将 undefined 设置为空的 ng-model 字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26138146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why angular sets undefined to empty ng-model fields
提问by Karol F
I have simple crud application in Angular (1.2) and Laravel (4.2). To simple crud operations I use efficient Eloquent method:
我在 Angular (1.2) 和 Laravel (4.2) 中有简单的 crud 应用程序。为了简单的 crud 操作,我使用了高效的 Eloquent 方法:
$product->fill(Input::all());
which takes all fields from request payload, but is there a problem when I need to do update model with empty fields.
它从请求有效负载中获取所有字段,但是当我需要使用空字段更新模型时是否存在问题。
In the edit action I have a form which is filled with the response of $resource get method of my service:
在编辑操作中,我有一个表单,其中填充了我的服务的 $resource get 方法的响应:
adminModule.factory 'Product', ($resource) ->
$resource '/admin/products/:id', { id: '@id' }, {
query: { method: 'GET', isArray: false }
update: { method: 'PUT' }
}
a controller:
控制器:
adminModule.controller 'ProductFormEditController', ($scope, Product, $stateParams) ->
$scope.formData = Product.get({id: $stateParams.id})
and html:
和 html:
<input type="text" data-ng-model="formData.name" name="name" class="form-control" id="name"
placeholder="Nazwa" data-server-error required>
If I clear this field value and do submit the value of $scope.formData is set to undefined and there is not included in the PUT request, so the Laravel model fill method doesn't see the field anyway and don't sets the empty value for the validation, but takes the original value of the model.
如果我清除此字段值并提交 $scope.formData 的值设置为 undefined 并且没有包含在 PUT 请求中,那么 Laravel 模型填充方法无论如何都看不到该字段并且不设置空验证值,但采用模型的原始值。
The problem is: How can I send the empty string from ngModel instead of undefined?
问题是:如何从 ngModel 而不是 undefined 发送空字符串?
p.s. If the input type is Textarea the $resource send empty "" string :-/, so I'm confusing...
ps 如果输入类型是 Textarea,则 $resource 发送空的 "" 字符串 :-/,所以我很困惑......
回答by Alexandre Junges
You can configure angular to set your model even when the value is not valid with the property allowInvalid
from ngModelOptions
.
甚至当值不与物业有效您可以配置的角度来设置你的模型allowInvalid
从ngModelOptions
。
allowInvalid
: boolean value which indicates that the model can be set with values that did not validate correctly instead of the default behavior of setting the model to undefined.
allowInvalid
: 布尔值,表示可以使用未正确验证的值来设置模型,而不是将模型设置为未定义的默认行为。
Example:
例子:
<input
type="text"
ng-model="myvalue"
ng-model-options="{ allowInvalid: true }"/>
回答by Stephen Friedrich
When you have the "required" directive on an input, an empty value will never be set (so it will be undefined).
当您在输入上使用“required”指令时,将永远不会设置空值(因此它将是未定义的)。
See the source code ("requiredDirective" in file input.js) where no value is retrieved to store in the model, when the input is blank:
请参阅源代码(文件 input.js 中的“requiredDirective”),其中当输入为空时,不会检索任何值以存储在模型中:
if (attr.required && ctrl.$isEmpty(value)) {
ctrl.$setValidity('required', false);
return;
} else {
ctrl.$setValidity('required', true);
return value;
}
IMHO it is a little strange use case anyway: You do want client side validation, but still want to submit invalid values to the server, right?
恕我直言,无论如何,这是一个有点奇怪的用例:您确实需要客户端验证,但仍想向服务器提交无效值,对吗?
I guess you can write your own validation ("my-required") to do that.
我想您可以编写自己的验证(“我需要”)来做到这一点。
Alternatively you could copy over default values before posting the data to the server, for example with lodash (or underscore):
或者,您可以在将数据发布到服务器之前复制默认值,例如使用 lodash(或下划线):
_.defaults(formData, { name: ""});
_.defaults(formData, { name: ""});