0

Been finding how to clear form data with the use of angular and $setPristine Function but still no results always gives me an error saying $setPristine is not a function. Can anyone kindly help me for a solution?

here's my 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'
                    });
                }


            });          
        } 

Here's the Html code

<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="[email protected]">
                                    </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>
2
  • Could you share the HTML code? Commented Jul 27, 2016 at 5:52
  • @pdorgambide added the html code. :) Commented Jul 27, 2016 at 5:58

5 Answers 5

2

You can remove form field value by removing value of ng-model like

Your code

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

            }; 

Replace with this

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

            }; 

Your code

customerForm.$setPristine(true);

Replace with this

$scope.CustDetails={};
Sign up to request clarification or add additional context in comments.

1 Comment

@AshaneAlvis any time
1

use:

$scope.$destroy 

as it removes all the children associated to the parent scope and will clear all the data associated with it.

Comments

0

Use

$scope.customerForm.$setPristine(true);

Give the form a name like

name= "CusDetails"

Then It will solve the problem.

1 Comment

Nope Then it says $scope.customerForm is undefined
0
  1. Use name attr instead of id. name="forName"
  2. The form reference inside Controller will be $scope.formName

Comments

0

You can set the form with the code below:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.