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

时间:2016-06-02 12:01:37

标签: javascript jquery angularjs

我是AngularJS的新手,并尝试做一个项目。我有一个完美的表格。但是,我应该做的只有一件事。添加客户后,我需要清除表单。因为,当用户想要添加第二个客户时,用户会看到先前输入的值。

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

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:-3)

您必须手动清除表单元素的值

这应该这样做:

delete $scope.newcustomer