繁体   English   中英

提交表单后如何在angular js中重置表单字段

[英]How to reset form fields in angular js after form submit

我正在使用angular js提交表单。 我想在提交后重置表单字段

DEMO_ANGULAR JS

   $scope.addUser = function() 
    {
        $scope.errors = [];
        $scope.msgs = [];

        $scope.errors.splice(0, $scope.errors.length); // remove all error messages
        $scope.msgs.splice(0, $scope.msgs.length);

        $http.post("../admin/users/add_user", {'fname': $scope.fname, 'lname': $scope.lname, 'uname': $scope.uname,'gender': $scope.gender,'email': $scope.email,'mobile': $scope.mobile,'pass': $scope.pass}
        ).success(function(data, status, headers, config) {

            if (data.msg != '')
            {
                $scope.msgs.push(data.msg);
                $scope.get_users();
                $scope.form_add.$setPristine();
                $scope.currentRecord={};
            }
            else
            {
                $scope.errors.push(data.error);
            }
        }).error(function(data, status) { // called asynchronously if an error occurs
// or server returns response with an error status.
            $scope.errors.push(status);
        });
    }

这是我添加用户的视图(HTML)表单:

<form name="form_add" method="post">
                     <ul>
                        <li style="color:red" ng-repeat="error in errors"> {{ error}} </li>
                    </ul>
                    <ul>
                        <li style="color:green" ng-repeat="msg in msgs"> {{ msg}} </li>
                    </ul>
                        <table class="table" cellspacing=10 cellpadding=10>
                        <tr>
                        <td>First Name: <input required type="text" ng-pattern="/[a-zA-Z]$/"  ng-model="fname"  name="fname" id="fname" value=""/><br/>
                        <span style="color:red" ng-show="form_add.fname.$dirty && form_add.fname.$invalid">
                                <span ng-show="form_add.fname.$error.required">First Name is required.</span>
                                <span ng-show="form_add.fname.$error.pattern">Invalid</span>
                        </span>
                        </td>
                        <td>Last Name: <input required type="text" ng-pattern="/[a-zA-Z]$/" ng-model="lname" name="lname" id="lname" value=""/><br/>
                        <span style="color:red" ng-show="form_add.lname.$dirty && form_add.lname.$invalid">
                                <span ng-show="form_add.lname.$error.required">Last Name is required.</span>
                                <span ng-show="form_add.lname.$error.pattern">Invalid</span>
                        </span>
                        </td>
                        <td>User Name: <input required type="text" ng-pattern="/^[a-zA-Z0-9]{6,15}$/"   ng-model="uname" name="uname" id="uname" value=""/><br/>
                        <span style="color:red" ng-show="form_add.uname.$dirty && form_add.uname.$invalid">
                                <span ng-show="form_add.uname.$error.required">User Name is required.</span>
                                <span ng-show="form_add.uname.$error.pattern">Invalid: Minimum 6 characters and maximum 15 characters</span>    
                        </span>
                        </td>
                        <td width="20%">Gender :<br/> <input type="radio"  ng-model="gender" name="gender" id="r1" value="m" checked />Male
                        &nbsp;&nbsp;<input type="radio"  ng-model="gender" name="gender" id="r2" value="f"/>Female</td>
                        </tr>
                        <tr>
                        <td>Email ID: <input type="text" required ng-pattern="/^[a-z]+[a-z0-9._]+@[a-z]+\.[a-z.]{2,5}$/"  ng-model="email" name="email" id="email" value=""/><br/>
                            <span style="color:red" ng-show="form_add.email.$dirty && form_add.email.$invalid">
                                <span ng-show="form_add.email.$error.required">Email is required.</span>
                                <span ng-show="form_add.email.$error.pattern">Invalid email address.</span>
                            </span>
                        </td>
                        <td>Mobile : <input type="text" required  ng-model="mobile" ng-pattern="/^[1-9]{1}[0-9]{9}$/"  name="mobile" id="mobile" value=""/><br/>
                        <span style="color:red" ng-show="form_add.mobile.$dirty && form_add.mobile.$invalid">
                                <span ng-show="form_add.mobile.$error.required">Mobile is required.</span>
                                <span ng-show="form_add.mobile.$error.pattern">10 digits required</span>
                        </span>
                        </td>
                        <td>Password : <input required type="password" ng-model="pass" name="pass" id="pass" value=""/>
                        <span style="color:red" ng-show="form_add.mobile.$dirty && form_add.mobile.$invalid">
                                <span ng-show="form_add.mobile.$error.required">Mobile is required.</span>
                                <span ng-show="form_add.mobile.$error.pattern">10 digits required</span>
                        </span>
                        </td>
                        <td><button type="button" class="btn btn-primary" ng-click='addUser();' ng-disabled="form_add.$invalid">Save</button></td>
                        </tr>

                        </table>

                    <br/>
                    </form>

提交后如何重置表格,以上代码有何变化? 而且我的问题还没有解决!

您可以为模型尝试基础对象。 看例子

<form action="">
    <label for="">Name</label>
    <input type="text" ng-model="user.model">
    <label for="">Email</label>
    <input type="text" ng-model="user.email">
    <label for="">Password</label>
    <input type="password" ng-model="user.password">
    <button type="submit">Submit</button>
</form>

用于Angular

$scope.addUser = function() 
    {
$scope.errors = [];
        $scope.msgs = [];

         $http.post("your/desire/url", yourData)
         .success(function(data, status, headers, config) {

            if (data.msg != '')
            {
                //do something
                //reseting form
                $scope.user='';
            }
            else
            {
                $scope.errors.push(data.error);
            }
        }).error(function(data, status) { 
            $scope.errors.push(status);
        });

    }

提交完表单后,您必须重置模型基础对象

$ scope.user = '';

我认为它将很好:)

您可以通过以下方式休息表格:

就像您的表单一样:

 <form novalidate class="css-form">
    Name: <input type="text" ng-model="user.name" required /><br />
    E-mail: <input type="email" ng-model="user.email" required /><br />
    Gender: <input type="radio" ng-model="user.gender" value="male" />male
    <input type="radio" ng-model="user.gender" value="female" />female<br />
    <input type="button" ng-click="reset()" value="Reset" />
    <input type="submit" ng-click="update(user)" value="Save" />
  </form>

因此,您的重置按钮的代码将类似于:

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

这肯定会很快起作用

其解决方法是: 我已使用此代码进行RESET:

$scope.fname='';
$scope.lname='';
$scope.uname='';
$scope.email='';
$scope.mobile='';
$scope.pass='';
$scope.form_add.$setPristine();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM