javascript - 一段时间后隐藏带有angularjs表单验证的错误标签

标签 javascript angularjs validation angularjs-directive angularjs-validation

我正在使用 angularjs 表单验证进行客户端验证。我需要在消息出现 3 秒后隐藏使用 angularjs 错误表单验证方法显示的标签。

HTML 看起来像这样,

<form name="userForm" novalidate>
  <div class="form-group">
    <label>Name</label>
    <input type="text" name="name" class="form-control" ng-model="name" required>
    <label ng-show="userForm.name.$invalid && !userForm.name.$pristine" class="help-block">You name is required.</label>
  </div>

  <div class="form-group">
    <label>Email</label>
    <input type="email" name="email" class="form-control" ng-model="email">
    <label ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Enter a valid email.</label>
  </div>
</form>

寻找通用解决方案,如自定义指令或其他。

提前致谢

最佳答案

我创建了一个这样的指令,

app.directive('timeoutHtml', function($timeout) {
    return {
        restrict: 'A',
        scope: {
            onVisible: '=showModel'
        },
        link: function(scope, element, attrs) {

            scope.$watch(function() {
                return attrs.ngShow;
            }, function(newValue, oldValue) {
                checkVisibility();
            });

            function checkVisibility() {
                if (element.is(':visible')) {
                    scope.$watch('onVisible', function(newValue, oldValue) {
                        console.log(newValue)
                        if (newValue === true) {
                            $timeout(function() {
                                scope.onVisible = false;
                            }, 2000);
                        }
                    });

                }
            };

        }
    };
});

并将 html 更改为此,

<form name="userForm" novalidate="">
      <div class="form-group">
        <label>Name</label>
        <input type="text" name="name" class="form-control" ng-model="name" required="" />
        <label  timeout-html="" show-model="submitted" ng-show="userForm.name.$invalid && submitted" class="help-block">You name is required.</label>
      </div>
      <div class="form-group">
        <label>Email</label>
        <input type="email" name="email" class="form-control" ng-model="email" required />
        <label ng-show="userForm.email.$invalid && submitted" class="help-block">Enter a valid email.</label>
      </div>
      <button type="submit" ng-click="submitted=true;">Submit</button>
</form>

Plunker 演示:https://plnkr.co/edit/P4uopx4MhOFEszXuuYyA?p=preview

关于javascript - 一段时间后隐藏带有angularjs表单验证的错误标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35633649/

相关文章:

javascript - 如何在 JavaScript 中验证包含 “00:07 PM” 的日期?

javascript - onchange jquery 文件类型验证

javascript - 网站加载时可以从确定的 div 开始吗?

javascript - 如何使用 Angular 5 按顺序运行 2 个 for 循环

javascript - Node.JS:出现错误:[nodemon] 内部监视失败:监视 ENOSPC

javascript - AngularJS 范围变量在另一个函数内更改时不会更新

javascript - 将动画帧更改为仅在坦克移动时

javascript - 在 ng-click 上更改 ng-include 中的模板

javascript - 在 AngularJS 中获取 JSON 数组的大小

java - 日验证返回错误