我经常听到$ 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未定义的错误。我也尝试过

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




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


如何引用此输入?

最佳答案

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

<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*);

09-20 13:07