AngularJS下拉需要验证 [英] AngularJS Dropdown required validation

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

问题描述

以下是我的code片段。我想用角度来验证我的下拉列表。

Following is my code snippet. I want to validate my dropdown using angular.

<td align="left" width="52%"> 
  <span class="requiredSmall">*</span> 
    <select class="Sitedropdown" style="width: 220px;" 
            ng-model="selectedSpecimen().serviceID" 
            ng-options="service.ServiceID as service.ServiceName for service in services"> 
         <option value="" ng-selected="selected">Select Service</option> 
   </select> 
</td>

有效手段:

有效值可以是任何东西,但选择服务,这是我的默认值。像其他ASP.net需要的字段校验器默认值=0下拉列表中,所以我在这里的下拉菜单会从服务的约束,我想选择除选择服务所有其他值。

推荐答案

您需要添加一个名称属性的下拉列表中,那么你需要添加一个要求属性,然后你可以使用 myForm会[输入名称]引用错误$ error.required

You need to add a 'name' attribute to your dropdown list, then you need to add a required attribute, and then you can reference the error using myForm.[input name].$error.required:

HTML

<form name="myForm" ng-controller="Ctrl">

  <select name="service_id" class="Sitedropdown" style="width: 220px;"          
          ng-model="ServiceID" 
          ng-options="service.ServiceID as service.ServiceName for service in services"
          required> 
    <option value="">Select Service</option> 
  </select> 
  <span ng-show="myForm.service_id.$error.required">Select service</span>

</form>

控制器:

function Ctrl($scope) {
  $scope.services = [
    {ServiceID: 1, ServiceName: 'Service1'},
    {ServiceID: 2, ServiceName: 'Service2'},
    {ServiceID: 3, ServiceName: 'Service3'}
  ];
}

这篇关于AngularJS下拉需要验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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