【问题标题】:Check if form is touched检查表单是否被触摸
【发布时间】:2016-07-28 10:22:00
【问题描述】:

在角度我想这样做

<script>
  //inside the controller
  $scope.myForm.$dirty{
    //hide some divs etc.
  }
</script>

如何仅在 JavaScript 中执行此操作,而不在我的 HTML 中进行任何验证?

更新

<form id="loginform" name="loginform" ng-submit="login()" novalidate>
<input type="text" class="form-control" id="username" name="username" ng-model="username" ng-click="hideInvalidUsername()" placeholder="Username" autofocus required>

</form>

在我的控制器中

  app.controller('myCtrl', ['$scope', 'localStorageService' ,
  function ($scope, localStorageService ) {  

if($scope.loginform.$dirty) { alert("yo"); }

【问题讨论】:

  • 将其写入 .js 文件,并将其包含在您的 html 中?

标签: angularjs forms validation


【解决方案1】:

你的问题不是很清楚,但我猜你想这样做:

<form name="myForm">
    <input type="text">
    <span class="message" ng-show="myForm.$dirty">the form is dirty</span>
</form>

表单将自动绑定到您的作用域中的一个具有name 属性的变量

【讨论】:

  • @Martjin 让我解释一下。我不会像 `ng-show="myForm.$dirty". I want to check if the form is touched using javascript and angular in my controller. Something like $scope.myForm.$dirty{` 那样用 HTML 编写代码。这可能吗?
  • 是的,当然,你可以这样做if($scope.myForm.$dirty) { doSomething(); }
  • 在 myForm.$dirty 上使用观察者表达式
  • @MartijnWelker 谢谢,但我得到了Cannot read property '$dirty' of undefinedmyForm 是表单的name,对吧?
  • @slevin 为什么不在上面放置一个观察者,像这样:$scope.$watch('myForm.$dirty', function(n) {if(n) {alert ('dirty'); }});
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多