如何清除或重置angularjs中表单中的数据?

时间:2016-07-27 05:28:30

标签: javascript html angularjs forms clear

已经找到了如何使用angular和$ setPristine函数清除表单数据但仍然没有结果总是给我一个错误,说$ setPristine不是一个函数。有人可以帮我解决问题吗?

这是我的angular.controller

$scope.AddCustomer = function () {

            var CustDetails = {
                cname: $scope.CusDetails.cname,
                comname: $scope.CusDetails.comname,
                tel: $scope.CusDetails.tel,
                email: $scope.CusDetails.email

            };           

            CustService.Customer(CustDetails, function (res) {
                console.log(res);

                $.extend($.gritter.options, {
                    position: 'bottom-right',
                });

                if (res.data == 'success') {
                    $.gritter.add({

                        title: 'Success!',
                        text: 'Successfully added the new customer ' + '<h4><span class="label label-primary">' + CustDetails.cname + '</span></h4>',
                        time: '5000',
                        close_icon: 'l-arrows-remove s16',
                        icon: 'glyphicon glyphicon-ok-circle',
                        class_name: 'success-notice'
                    });

                    //CustDetails = {};

                    customerForm.$setPristine(true);
                }
                else {
                    $.gritter.add({
                        title: 'Failed!',
                        text: 'Failed to add a new customer',
                        time: '5000',
                        close_icon: 'l-arrows-remove s16',
                        icon: 'glyphicon glyphicon-remove-circle',
                        class_name: 'error-notice'
                    });
                }


            });          
        } 

这是Html代码

<div ng-controller="AddCustomerController">
    <div class="page-content-wrapper">
        <div class="page-content-inner">
            <div id="page-header" class="clearfix">
                <div class="page-header">
                    <h2>Add Customer</h2>
                    <span class="txt">Create and add new customer.</span>
                </div>
            </div>

           <!--Start .row-->
            <div class="row">
                <div class="col-md-1">
                </div>

                <div class="col-lg-9 col-sm-9 col-xs-12">
                    <!--col-lg-9 starts here-->
                    <div class="panel panel-default toggle panelMove panelClose panelRefresh">
                        <div class="panel-heading">
                            <h4 class="panel-title">Customer Details</h4>
                        </div>
                        <div class="panel-body pt0 pb0">
                            <form class="form-horizontal group-border stripped" id="customerForm">
                                <div class="form-group">
                                    <label class="col-lg-2 col-md-3 control-label">Customer Name</label>
                                    <div class="col-lg-10 col-md-9">
                                        <input type="text" required id="cname" ng-model="CusDetails.cname" class="form-control" name="cname" />
                                    </div>
                                </div>
                                <!--end of .form-group-->
                                <div class="form-group">
                                    <label class="col-lg-2 col-md-3 control-label">Company Name</label>
                                    <div class="col-lg-10 col-md-9">
                                        <input type="text" required id="comname" ng-model="CusDetails.comname" class="form-control" name="comname" />
                                    </div>
                                </div>
                                <!--end of .form-group-->
                                <div class="form-group">
                                    <label class="col-lg-2 col-md-3 control-label" for="">Telephone Number</label>
                                    <div class="col-lg-10 col-md-9">
                                        <div class="input-group input-icon">
                                            <span class="input-group-addon"><i class="fa fa-phone s16"></i></span>
                                            <input ng-model="CusDetails.tel" class="form-control" id="ctel" type="text" placeholder="(999) 999-9999">
                                        </div>
                                    </div>
                                </div>
                                <!-- End .form-group  -->
                                <div class="form-group">
                                    <label class="col-lg-2 col-md-3 control-label" for="">Email address</label>
                                    <div class="col-lg-10 col-md-9">
                                        <input id="email" ng-model="CusDetails.email" type="text" class="form-control" name="placeholder" placeholder="someone@example.com">
                                    </div>
                                </div>
                                <!-- End .form-group  -->
                            </form>
                        </div>
                    </div>
                    <!--End .panel-->
                </div>
                <!--.col-9 ends here-->
            </div>
            <!--End .row-->
            <!--Start .row-->
            <div class="row">
                <div class="col-md-1"></div>
                <div class="col-lg-9 col-sm-9 col-xs-12">
                    <button id="btnSubmit" type="submit" ng-click="AddCustomer()" class="btn btn-info pad"><span class="fa fa-user-plus"></span> Add Customer</button>
                    <button type="submit" class="btn btn-default pad">Cancel</button>
                </div>
            </div>
        </diV>
    </div>
</div>

5 个答案:

答案 0 :(得分:2)

您可以通过删除ng-model的值来删除表单字段值,如

  

您的代码

$scope.CustDetails = {
                cname: $scope.CusDetails.cname,
                comname: $scope.CusDetails.comname,
                tel: $scope.CusDetails.tel,
                email: $scope.CusDetails.email

            }; 
  

替换为

customerForm.$setPristine(true);
  

您的代码

$scope.CustDetails={};
  

替换为

$(document).ready

答案 1 :(得分:1)

使用:

$scope.$destroy 

因为它删除了与父作用域关联的所有子项,并将清除与其关联的所有数据。

答案 2 :(得分:0)

使用

$scope.customerForm.$setPristine(true);

为表单命名,如

name= "CusDetails"

然后它将解决问题。

答案 3 :(得分:0)

  1. 使用名称 attr而不是id。 name="forName"
  2. Controller内的表单引用将为$scope.formName

答案 4 :(得分:0)

您可以使用以下代码设置表单:

$scope.customerForm.$setPristine();
$scope.customerForm.$setUntouched();
$scope.CustDetails={};