如何使用$ touch所需的ng?

时间:2016-12-06 10:36:11

标签: angularjs angularjs-validation

如果另一个输入已更改,我会尝试根据需要设置输入。如果密码已更改,我需要确认密码。

我在输入确认密码时使用此功能,但绝不需要:

ng-required="userForm.Password.$touched"

CodePen

另一个问题:有没有办法删除ng-Model="user.ConfirmPassword"?因为在我的模型"用户"上不需要ConfirmPassword,不是吗?

2 个答案:

答案 0 :(得分:2)

您应该使用<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet" /> <span class="glyphicon glyphicon-plus"></span>属性而不是name

此外,这不是有效的语法:

id

应该是:

<span class="error" ng-show="userForm.Password.$touched  userForm.Password.$error.required">Password is required</span>

然后它会正常工作,只要<span class="error" ng-show="userForm.Password.$touched">Password is required</span> 是输入元素的名称,而不是ID。

答案 1 :(得分:1)

您没有使用name属性,因此您无法使用密码和ConfirmPassword的表单属性...
同时将AND更改为OR ..

这里是更新的代码

<form name="userForm" ng-controller="userController">
  <label for="Password">Set a password:</label>
  <input type="password" name="Password" id="Password" ng-model="user.Password" required />
  <span class="error" ng-show="userForm.Password.$touched">Password is required</span>
  <br />
  <label for="ConfirmPassword">Confirm the password:</label>
  <input name="ConfirmPassword" type="password" id="ConfirmPassword" ng-model="user.ConfirmPassword" ng-required="userForm.Password.$touched" />
  <span class="error" ng-show="userForm.Password.$touched || userForm.ConfirmPassword.$touched || userForm.ConfirmPassword.$error.required">Confirm password is required</span>
  <br/>
  <span>Password: {{user.Password}}</span>
  <br/>
  <span>Conform Password: {{user.ConfirmPassword}}</span>
</form>