javascript - AngularJS 自定义验证 - 在 Controller 中将表单项设置为无效

标签 javascript angularjs

我希望快速向我的应用程序引入一些功能,如果不满足特定的验证标准(例如字数至少为 20),我们会将表单项设置为无效。我不确定是否应该使用 filterdirective 或者只是在 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/

相关文章:

javascript - 包含函数的返回值应为 null 或模板

javascript - AngularJs:从下拉列表中选择值

java - Java 小程序部署工具包 : how to avoid the redirect when no JVM is installed

javascript - jquery dataTables - 如何添加编辑和删除选项

javascript - 所有 div 中显示的错误消息

javascript - 使用 inboxSDK 获取发件人电子邮件

javascript - $http 响应 Set-Cookie 不可访问

javascript - angular watchers 和 event listeners 以什么顺序执行?

angularjs - 从 ng-value 指令更新 ng-model

javascript - $watchCollection 未被触发