如何使用$ setValidity设置输入验证而不使用$ scope

时间:2016-07-26 16:54:33

标签: javascript angularjs

我经常听说$ scope不再是最佳实践。在任何情况下,我们都使用控制器作为语法,甚至不将$ scope作为依赖项传递。

<form name="passwordForm" id="passwordForm">
   <input type="password" id="newPassword" name="newPassword" ng-minlength="8" data-ng-model="vm.pwdata.newPassword" required>

$scope.passwordForm.newPassword.$setValidity(*my conditions here*);

当然这里我得到$ scope undefined错误。我也试过了

passwordForm.newPassword.$setValidity(*my conditions here*); 

vm.passwordForm.newPassword.$setValidity(*my conditions here*);

如何引用此输入?

2 个答案:

答案 0 :(得分:0)

您应该将表单名称设置为控制器实例属性。

<form ng-controller="theController as VM" name="VM.passwordForm" id="passwordForm">
    <input type="password" id="newPassword" name="newPassword" ng-minlength="8" data-ng-model="vm.pwdata.newPassword" required>
控制器中的

this.passwordForm.newPassword.$setValidity(*my conditions here*);

答案 1 :(得分:0)

你在州/路线/模板上有控制器吗?如果是 - 在模板内部使用此变量,在控制器中使用“this”:

var vm = this;
vm.password .....