AngularJs不验证无效下拉菜单选择 [英] AngularJs doesn't validate a invalid dropdown selection
问题描述
下面是一个基本的plunker演示该问题。
Here is a basic plunker that demonstrates the problem.
-
当你设置DropDownList / select元素模型清空您接收到所需的错误信息
When you set the dropdownlist/select element model to empty you receive the required error message
但是,当您设置控制器模型,该模型是不是在提供NG-选项中选择元素无法显示所需要的选项。但是下拉/选择是处于无效状态
But when you set the model from controller, and the model is not a option in the provided ng-options select element fails to show required. But the dropdown/select is in invalid state
Plunker
http://plnkr.co/edit/gLtjRwkaaBOQG7YMvDav?p=$p$ PVIEW
那么,我们如何去解决这个问题呢?
So how do we go about solving this problem?
推荐答案
参考文档 - 滚动至底部到 $误差
部分
Reference Documentation - Scroll to the bottom to the $error
section.
$误差
仅验证的属性选择
标签,而不是选项进行选择。
$error
only validates attributes of the select
tag and not the option selected.
要求
是选择标签的属性 - 因此,当下拉是空的, $误差
标志设置为true。
required
is an attribute of the select tag - thus when the dropdown is empty, the $error
flag is set to true.
然而,分配一个选项,是不是predefined选项不被 $误差
处理之中 - 你必须处理这个使用类似自己:
However, assigning an option that is not among the predefined options is not handled by $error
- you have to handle this yourself using something like:
<input type="button" value="Submit" ng-click="submitForm($event)">
和在你的控制器:
$scope.submitForm = function (event) {
event.preventDefault();
//Do your validation on the select value
//If everything is fine, call the actual submit function
};
这篇关于AngularJs不验证无效下拉菜单选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!