Clear input field on button click in Angular

Issue

I know this is a common question , but I am facing problem in this. I am unable to refresh by input fields on button click.

When I am clicking button , the information is sent and added up in a list. So whenever I click it the input fields must get cleared/refresh (not page load).

View:

<div class="input-group">
        <span class="input-group-addon" id="reg_input">Name</span>
         <input type="text" class="form-control" placeholder="Name" ng-model="ff.Name" required>
</div>

<div class="input-group">   
    <span class="input-group-addon" id="reg_input">Block</span>
     <input class="form-control" id="bl" type="text" ng-model="ff.Block" required>
</div>

<div class="input-group">   
    <input type="text" class="form-control" id="ip" type="text" ng-model="ff.IP" ng-maxlength="2" style="width: 30px" required>
</div>
   <a class="btn btn-md btn-primary" ng-click="getClick(ff)">Add</a>

Is there any refresh predefined in bootstrap button ?

EDIT controller:-

$scope.list = {};
                $scope.array_of_Names = [];
              $scope.getClick= function() {
                 $scope.master = angular.copy($scope.ff);
                  $http.post("url", $scope.list).success(function(data) { 
                        $scope.AllData= data;
                        $scope.addInfo.Segments.push($scope.list);
                        $scope.ff.Name = "";
                        $scope.ff.Block= "";
                        $scope.ff.IP= "";
                        $scope.array_of_Names.push($scope.list);
                       console.log("Segment successfully created");

                  },function (data, status, headers, config) { 
                       // growl.error("Something went wrong");
                  });
                     console.log($scope.master);
                };

Solution

Just Try it!. All property under $scope.ff will be reset.

$scope.ff={};

And you can apply it in your code part like this:

$scope.list = {};
            $scope.array_of_Names = [];
          $scope.getClick= function() {
             $scope.master = angular.copy($scope.ff);
              $http.post("url", $scope.list).success(function(data) { 
                    $scope.ff={};
                    $scope.AllData= data;
                    $scope.addInfo.Segments.push($scope.list);
                    $scope.array_of_Names.push($scope.list);
                   console.log("Segment successfully created");

              },function (data, status, headers, config) { 
                   // growl.error("Something went wrong");
              });
                 console.log($scope.master);
            };

Answered By – sebu

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.