javascript - 如何在没有 $scope 的情况下使用 $setValidity 设置输入验证

标签 javascript angularjs

我经常听说 $scope 不再是最佳实践。无论如何,我们使用 Controller 作为语法,甚至不传入 $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*);

如何引用此输入?

最佳答案

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

<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>

在 Controller 中

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

关于javascript - 如何在没有 $scope 的情况下使用 $setValidity 设置输入验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38595780/

相关文章:

javascript - 如何使用 Angularjs 编辑输入字段?

javascript - 使用 reactjs 和 babel 导出函数

javascript - HTML5 Canvas javascript 锥形画笔

javascript - 为什么我的所有 DOM 元素属性都返回为空字符串?

javascript - 使用 websockets 通过本地主机连接远程主机

html - 将值从 Express NodeJS 传递到 AngularJS Controller 变量

angularjs - 从 Cypress 访问 ng-if 值

php - 如何在 Laravel PHP 框架中合并两个集合而不删除(丢失)键?

javascript - 如何通过javascript中的按钮单击获取函数的值?

javascript - AngularJS - 选择的 ng-model 无法正常工作