简体   繁体   English

在AngularJS中提交后如何重置表格

[英]How to reset form after submit in AngularJS

I am new to AngularJS and trying to do a project. 我是AngularJS的新手,正在尝试做一个项目。 I have a form which works perfectly. 我有一个完美的表格。 However, there is only one thing that I should do. 但是,我只应该做一件事。 After adding a customer, I need to clear the form. 添加客户后,我需要清除表单。 Because, when the user wants to add a second customer, tthe user sees the previously entered values. 因为,当用户想要添加第二个客户时,该用户会看到先前输入的值。

$scope.add = function () {
        $scope.loading = true;
        $http.post('/api/Customer/', this.newcustomer).success(function (data) {
            alert("Added Successfully!!");
            $scope.addMode = false;
            $scope.customers.push(data);
            $scope.loading = false;

        }).error(function (data) {
            $scope.error = "An Error has occured while Adding Customer! " + data;
            $scope.loading = false;
        });
    };

Try this: 尝试这个:

$scope.add = function () {
        $scope.loading = true;
        $http.post('/api/Customer/', this.newcustomer).success(function (data) {
            alert("Added Successfully!!");
            $scope.addMode = false;
            $scope.customers.push(data);
            $scope.loading = false;
            this.newcustomer = {};

        }).error(function (data) {
            $scope.error = "An Error has occured while Adding Customer! " + data;
            $scope.loading = false;
        });
    };

You have to manually clear the values of the form elements 您必须手动清除表单元素的值

This should do it: 应该这样做:

delete $scope.newcustomer

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

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