How to reset form with angular

this is my code:

function CustomersController ($scope, $http) {
    $http.get('/ci_angular/api/customers/').success(function(customers){
        $scope.customers = customers;
    });

    $scope.addCustomer = function(){
        var customer = {
            customerName: $scope.customerNameText,
            email: $scope.emailText,
            address: $scope.addressText,
            city: $scope.cityText,
            state: $scope.stateText,
            postalCode: $scope.postalCodeText,
            country: $scope.countryText,
        };

        $scope.customers.push(customer);

        // update 1

        $http.post('/ci_angular/api/customers/customer', customer);

        $scope.emptyCustomers= {};

        $scope.reset = function() {
            $scope.customers = angular.copy($scope.emptyCustomers);
        };

        // update 2

        $http.post('/ci_angular/api/customers/customer', customer)
            .success(function() {           
                 $scope.emptyCustomers= {};

                 $scope.customerForm.$setPristine();
            }
        );
    }
}

After the success of the post, I want to reset the entire form field, but it does not work, my form name is CustomerForm, is there something I missed? thanks in advance

+4
source share
3 answers

If you want to reset your dirty flag form $ fields after ajax is successful, you must call setPristine () from within your success handler:

    $http.post('/ci_angular/api/customers/customer', customer)
        .success(function() {           
             $scope.customerForm.$setPristine();
        });

If you want to clear form fields, you must initialize the model inside your controller so that you can bind them to the view and clear them later:

Controller:

function CustomersController ($scope, $http) {
    // initialize an empty customer. 
    $scope.customer = {};
    // its good practice to initialize your customers array too
    $scope.customers = [];

    $http.get('/ci_angular/api/customers/').success(function(customers){
        $scope.customers = customers;
    });

    $scope.addCustomer = function(customer){

        $http.post('/ci_angular/api/customers/customer', customer).success(function() {
            // push the customers to the array only when its successful
            $scope.customers.push(customer);

            // clearing the customer form is easy now, just set it to an empty object
            $scope.customer = {};
        });

    }
}

var app = angular.module('app', []);
app.controller('CustomersController', CustomersController);

HTML

<div ng-app="app" ng-controller="CustomersController">
     <form name="customerForm">
            customerName:<input type="text" name="name" ng-model="customer.name" /><br />
            email: <input type="text" name="email" ng-model="customer.email" /><br />
            address: <input type="text" name="address" ng-model="customer.address" /><br />
            city: <input type="text" name="city" ng-model="customer.city" /><br />
            state: <input type="text" name="state" ng-model="customer.state" /><br />
            postalCode: <input type="text" name="postalcode" ng-model="customer.postalCode" /><br />
            country: <input type="text" name="country" ng-model="customer.country" /><br />

        <button ng-click="addCustomer(customer)">Add Customer</button>
     </form>
</div>
+8
source


    $scope.addCustomer = function(){
      var customer = {
        customerName: '',
        email: '',
        address: '',
        city: '',
        state: '',
        postalCode: '',
        country: '',
      };

Put this code in your success function.

+1
source

reset html reset $scope, :

HTML

<button ng-click="reset()">RESET</button>

JS

$scope.emptyCustomers= {};

$scope.reset = function() {
    $scope.customers = angular.copy($scope.emptyCustomers);
};

Check that the corner docs file is simple in shape for a similar example: https://docs.angularjs.org/guide/forms

Hope this helps!

0
source

All Articles