我希望快速向我的应用程序引入一些功能,如果不满足特定的验证标准(例如字数至少为 20),我们会将表单项设置为无效。我不确定是否应该使用 filter
、directive
或者只是在 controller
中执行逻辑,例如,这是我的表单以及我要执行的操作到目前为止已完成:
<form name="registerForm" data-ng-submit="publishReview(review)" novalidate autocomplete="off">
<textarea name="review-text" data-ng-model="review.description" required></textarea>
<!-- more stuff -->
</form>
现在我已将以下内容添加到我的 Controller 中:
$scope.$watch('review.description', function(newVal) {
if(typeof(newVal) !== 'undefined' && newVal.match(/\w+/g) !== null && newVal.match(/\w+/g).length > 20) {
// set to true
} else {
// set to false
}
})
;
现在我不确定如何将该项设置为 false/无效,我目前正在此处查看 NgModelController 上的 AngularJS 文档 https://docs.angularjs.org/api/ng/type/ngModel.NgModelController ,同时我又觉得把它放入我的 Controller 中感到不安?好像这是不好的做法?尽管在 Controller 中我可以轻松设置与验证逻辑关联的 ng-show/hide 条件的值。
任何想法、想法、意见和答案都表示赞赏。
最佳答案
This是学习如何在 Angular JS 中验证表单和其他内容的很好的教程
关于javascript - AngularJS 自定义验证 - 在 Controller 中将表单项设置为无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25263209/