AngularJS Dropdown需要验证

Dha*_*hak 61 javascript asp.net angularjs angularjs-validation

以下是我的代码段.我想用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>
Run Code Online (Sandbox Code Playgroud)

有效方式:

有效值可以是"选择服务"之外的任何值,它是我的默认值.像其他ASP.net要求字段验证器DefaultValue ="0"用于下拉列表,所以这里我的下拉列表将从服务绑定,我想选择除"选择服务"之外的所有其他值.

Ste*_*wie 117

您需要在下拉列表中添加"名称"属性,然后需要添加name属性,然后您可以使用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');
    };
        }
Run Code Online (Sandbox Code Playgroud)

控制器:

        <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');
    };
        }
Run Code Online (Sandbox Code Playgroud)

这是一个工作的plunker.

  • 应该提到的是,select需要ng-model才能工作 (7认同)
  • 正确,ngOptions需要ngModel. (3认同)
  • 您已使用Dirty,但直接按下提交按钮的用户将看不到此消息.如果在提交表单时设置showmessage之类的参数并使用此参数而不是脏,则效果会更好. (2认同)
  • 我对这个例子有疑问.我有一个提交按钮.它适用于我将所选选项更改为非默认选项,然后切换回默认值并尝试提交.错误消息msg正确显示.但是,如果我进入屏幕并立即单击提交按钮,即使默认选项是...默认选择,也不会显示错误消息. (2认同)
  • 我可以通过设置变量我为NG-模型选择为null,具有NG-显示为克服这一(formName.myoption $脏|| isSubmitted)&& formName.myoption.$ error.required.单击"提交"按钮时,isSubmitted设置为true. (2认同)