1

I am new to AngularJS and trying to do a project. 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;
        });
    };
7
  • 1
    Just set this.newcustomer ={}. Commented Jun 2, 2016 at 12:05
  • 1
    $scope.customer={}; Commented Jun 2, 2016 at 12:06
  • @charlietfl, it didnt work Commented Jun 2, 2016 at 12:06
  • Show some relevant html for your ng-models Commented Jun 2, 2016 at 12:07
  • Duplicate of stackoverflow.com/questions/13085024/…? Commented Jun 2, 2016 at 12:11

2 Answers 2

1

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;
        });
    };
Sign up to request clarification or add additional context in comments.

1 Comment

this is not what you think it is. Different context in the callback
-3

You have to manually clear the values of the form elements

This should do it:

delete $scope.newcustomer

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.