AngularJs不验证无效下拉菜单选择 [英] AngularJs doesn't validate a invalid dropdown selection

查看:122
本文介绍了AngularJs不验证无效下拉菜单选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个基本的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆