下面是我的代码片段.我想用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>

有效方式:

Valid values can be anything but "Select Service", it is my default value. Like other ASP.net Require field validator DefaultValue="0" for dropdown, so here My dropdown will be bound from the services and I want to select all other values except "Select Service".

推荐答案

您需要在下拉列表中添加name属性,然后添加required属性,然后可以使用myForm.[input name].$error.required引用错误:

HTML:

        <form name="myForm" ng-controller="Ctrl" ng-submit="save(myForm)" novalidate>
        <input type="text" name="txtServiceName" ng-model="ServiceName" required>
<span ng-show="myForm.txtServiceName.$error.required">Enter Service Name</span>
<br/>
          <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>

    Controller:

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

    $scope.save = function(myForm) {
    console.log('Selected Value: '+ myForm.service_id.$modelValue);
    alert('Data Saved! without validate');
    };
        }

这是一个有效的plunker.

Asp.net相关问答推荐

502 DotNet WebApplication的网关nginx已损坏

内容根路径在 .NET 6 中如何工作?

为什么 @Html.EditorFor 和 @Html.PasswordFor 在 MVC 中创建不同的样式框?

Windows 命令行中的/p:是什么意思

单个方法(即扩展方法)之间的调用不明确

SignalR 不在服务器上使用 Session

Asp.Net 会话在 ashx 文件中为空

ASP.net 页面在导入语句上出现错误,但我确实有参考?

对布尔查询字符串参数使用true或1

System.Linq.Dynamic 不支持 OrderByDescending("someColumn")?

如何设置asp.net身份cookie过期时间

排序下拉列表? - C#,ASP.NET

回发后动态创建的控件丢失数据

asp.net 有控制台日志(log)吗?

ASP .NET 单例

ASP.NET 身份,需要强密码

使用 ASP.NET 进行重定向后获取

配置授权服务器端点

如何在服务器控件属性中使用 ASP.NET <%= 标签?

Appdomain 回收究竟是什么