Angular:仅在关闭表单

时间:2016-09-17 16:44:54

标签: javascript html angularjs

https://jsfiddle.net/jzhang172/xu93yubL/

特殊问题出在我的电子邮件输入字段中 是否可以仅在字段验证后以及用户不在输入字段内时显示ng-show消息

换句话说,如果用户在输入字段上,我希望ng-show消息消失。

我已尝试过多种逻辑组合,并且无法让它发挥作用:



(function(angular) {
  'use strict';
angular.module('formExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.master = {};

    $scope.update = function(user) {
      $scope.master = angular.copy(user);
    };

    $scope.reset = function(form) {
      if (form) {
        form.$setPristine();
        form.$setUntouched();
      }
      $scope.user = angular.copy($scope.master);
    };

    $scope.reset();
  }]);
})(window.angular);

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>


<body ng-app="formExample">
  <div ng-controller="ExampleController">
  <form name="form" class="css-form" novalidate>
    Name:
    <input type="text" ng-model="user.name" name="uName" required="" />
    <br />
    <div ng-show="form.$submitted || form.uName.$touched">
      <div ng-show="form.uName.$error.required">Tell us your name.</div>
    </div>

    E-mail:
    <input type="email" ng-model="user.email" name="uEmail" required="" />
    <br />
    <div ng-show="form.$submitted || form.uEmail.$touched">
      <span ng-show="form.uEmail.$error.required" style="background:red;">Tell us your email.</span>
      <span ng-show="form.uEmail.$error.email"style="background:red;">This is not a valid email.</span>
    </div>


  </form>

</div>
</body>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

您可以使用 ngFocus ngBlur 指令来获得所需的结果......

在ngFocus上设置一些变量false并使用ngBlur设置为true并将该变量添加到ngShow条件中...

 <input type="email" ng-model="user.email" name="uEmail" required="" ng-focus="showEmailValidationMsg = false;" ng-blur="showEmailValidationMsg = true;"/>
    <br />
    <div ng-show="(form.$submitted || form.uEmail.$touched) && showEmailValidationMsg">
      <span ng-show="form.uEmail.$error.required" style="background:red;">Tell us your email.</span>
      <span ng-show="form.uEmail.$error.email"style="background:red;">This is not a valid email.</span>
    </div>

这里正在运作JSFIDDLE ...