angularjs形式隐藏取消隐藏触发器验证

时间:2015-11-29 10:32:06

标签: angularjs forms validation hide

我有一个带有验证设置的angularjs表单。我想点击显示按钮取消隐藏它,并在点击隐藏按钮时隐藏它。 如果我使用输入字段并隐藏然后再取消隐藏,我仍然会看到我不想要的验证消息。请帮我解决这个问题。

代码如下: 的index.html

<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
    <!-- CSS ===================== -->
    <!-- load bootstrap -->
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"> 
    <style>
        body    { padding-top:30px; }
    </style>

    <!-- JS ===================== -->
    <!-- load angular -->
    <script src="http://code.angularjs.org/1.2.6/angular.js"></script> 
    <script src="script.js"></script>
</head>

<!-- apply angular app and controller to our body -->
<body ng-app="validationApp" ng-controller="mainController">
<div class="container">
<div class="col-sm-8 col-sm-offset-2">

    <!-- PAGE HEADER -->
    <div class="page-header"><h1>AngularJS Form Validation</h1></div>

    <!-- FORM -->
    <!-- pass in the variable if our form is valid or invalid -->
    <button type="button" ng-click="unhide()" class="btn btn-primary">Show</button>
    <form ng-show="showForm" name="userForm" ng-submit="submitForm(userForm.$valid)" novalidate> <!-- novalidate prevents HTML5 validation since we will be validating ourselves -->

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

<!-- USERNAME -->
<div class="form-group">
  <label>Username</label>
  <input type="text" name="username" class="form-control" ng-model="user.username" ng-minlength="3" ng-maxlength="8">
  <p ng-show="userForm.username.$error.minlength" class="help-block">Username is too short.</p>
  <p ng-show="userForm.username.$error.maxlength" class="help-block">Username is too long.</p>
</div>

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

        <!-- SUBMIT BUTTON -->
        <button type="submit" class="btn btn-primary">Submit</button>
        <button type="button" ng-click="hide()" class="btn btn-primary">Hide</button>

    </form>

</div><!-- col-sm-8 -->
</div><!-- /container -->
</body>
</html>

的script.js:

// app.js
// create angular app
var validationApp = angular.module('validationApp', []);

// create angular controller
validationApp.controller('mainController', function($scope) {

  // function to submit the form after all validation has occurred            
  $scope.submitForm = function(isValid) {
alert(isValid);
    // check to make sure the form is completely valid
    if (isValid) {
      alert('our form is amazing');
    }

  };
  $scope.hide = function(){
    $scope.showForm = false;
  }

  $scope.unhide = function(){
    $scope.showForm = true;
    $scope.userForm.$setUntouched();
  }
});

以下是plunker链接: http://plnkr.co/49k8P0

2 个答案:

答案 0 :(得分:1)

要实现预期的行为,您需要在代码中进行一些更改。

  1. 所有表单字段都应属于一个对象,例如创建一个$scope.user = {}对象,然后将所有与用户相关的字段放在user对象中,如user.username,{{1} }和user.name这样,在清除表单时,您可以直接执行user.email
  2. 隐藏时,您需要清除user = {}对象,此处表单对象将是form form的名称。
  3. 更简单的解决方案是使用userForm而不是ng-if。这将根据ng-show值添加和删除DOM。

    Demo Here

答案 1 :(得分:0)

如果您仍希望将先前输入的数据保留在字段中但仅清除消息,则将变量添加到提交表单时设置为true的范围。验证消息仅在变量为true时显示。然后,您可以在隐藏时将其设置为false。这意味着当再次显示表单时,消息将被隐藏。

  $scope.hide = function(){
     $scope.showForm = false;
     $scope.submitted = false;
  }

Plunker:http://plnkr.co/edit/dnBu0mD9RLvLdVJJKBGe