AngularJs验证特定选择

时间:2017-01-19 07:00:47

标签: html angularjs

我有一个下拉列表,当我选择'one'时,我的输入就出现了。该输入是必填字段。我想要的是当用户选择'one'然后输入的验证工作,因此在保存按钮上它显示错误但如果它选择其他值或没有值然后保存它不显示验证错误。

<form name="myForm" data-ng-submit="save(myForm.$valid)"novalidate="novalidate">
  <div class="form-group">
      <label class="col-md-6">Select:</label>
      <select class="col-md-2" ng-model="val.value">
            <option value="1" selected>one</option>
            <option value="2">two</option>
            <option value="3">three</option>
            <option value="4">four</option>
      </select>               
      <div ng-show="val.value=='1'">
         <label class="col-md-3">one:</label>
         <input type="number" class="col-md-4" ng-model="val.one" name="valOne" data-ng-class="{'has-error':myForm.valOne.$dirty && myForm.valOne.$invalid }" required="required"/>
         <div class="col-md-2" data-ng-messages="myForm.valOne.$error"
         data-ng-show="myForm.valOne.$touched ||myForm.$submitted">
            <div class="error-message" data-ng-message="required">Please enter Value
            </div>
         </div>
     </div>                                
   </div>
   <button type="submit" class="btn btn-warning"><i class="fa fa-save"></i>
       Save</button>
</form>

请推荐一些东西。

1 个答案:

答案 0 :(得分:0)

您可以使用ng-required:

 <input type="number" class="col-md-4" ng-model="val.one" name="valOne" data-ng-class="{'has-error':myForm.valOne.$dirty && myForm.valOne.$invalid }" ng-required="val.value == 1"/>