如何使用角度重置表单

时间:2014-09-17 02:27:03

标签: angularjs reset

这是我的代码:

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();
            }
        );
    }
}

发布成功后,我想重置所有表单字段,但它不起作用, 我的表格名称是customerForm,有什么我错过的吗?提前谢谢

3 个答案:

答案 0 :(得分:8)

如果你想在成功的ajax调用之后重置表单字段$ dirty flags,那么你应该从成功处理程序内部调用setPristine():

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

如果要清除表单字段,则应初始化控制器内的模型,以便将它们绑定到视图,然后将其清除:

控制器:

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>

答案 1 :(得分:1)


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

将此代码放入您的成功函数中。

答案 2 :(得分:0)

您可以尝试在html上添加重置按钮,并在$ scope中重置功能,如下所示:

<强> HTML

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

JS添加内部控制器

$scope.emptyCustomers= {};

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

检查Angularjs docs简单表单以获取类似示例:https://docs.angularjs.org/guide/forms

希望这有帮助!