setValidity无法使用ng-show

时间:2015-12-28 23:16:22

标签: angularjs

我的setValidity不起作用,消息ngshow运行不正常。

HTML:

<span ng-show="form.year_of_birth.$error.notAge &&
              form.year_of_birth.$touched">Your to Young</span>

JS:

$scope.form.year_of_birth.$setValidity('notAge',false);

1 个答案:

答案 0 :(得分:0)

要创建各种检查并更改输入表单的有效性,可以使用use-form-error指令。

在你的情况下这样做(jsfiddle):

&#13;
&#13;
<form name="ExampleForm">
  <label>Enter number more then 7:</label>
  <input ng-model="dateBirth" name="dateBirth" use-form-error="notAge" use-error-expression="dateBirth&&dateBirth<7" />
  <div ng-show="ExampleForm.dateBirth.$error.notAge">Your to young</div>
</form>
&#13;
&#13;
&#13;

或者您可以使用ngMessages:

&#13;
&#13;
     <form name="ExampleForm">
  <label>Enter number more then 7 and less then 70:</label>
  <input ng-model="dateBirth" name="dateBirth" use-form-error="notAge" use-error-expression="dateBirth&&dateBirth<7" />
  <span use-form-error="isOld" use-error-expression="dateBirth&&dateBirth>70" use-error-input="ExampleForm.dateBirth"></span>
  <div ng-messages="ExampleForm.dateBirth.$error" class="errors">
    <div ng-message="notAge">Your a young</div>
    <div ng-message="isOld">Your a old</div>
  </div>
</form>
&#13;
&#13;
&#13;