Blessan Kurien Blessan Kurien - 2 months ago 6
AngularJS Question

Angularjs:ng-model binding doesn't work after the first AJAX request

I am making a post request via angularjs.

<input class="form-control" ng-model="hotel.hotel_name" type="text">
<input class="form-control" ng-model="hotel.hotel_loc" type="text">
<input type="button" value="Submit" name="Submit" ng-click="addHotel(hotel)" class="btn btn-sm btn-primary">


controller

$scope.addHotel=function(hotel){
$http({
method:"POST",
data:{'hotel':hotel},
url:base_url+'operator/hotel/add_hotel'
}).then(function serverResponse(response){
//clear the old form data
$scope.hotel=[];
})
}


Once the data processed successfully clear the old form data
$scope.hotel=[]


But when i resubmit the form with some values it can't read i will get
undefiend


$scope.addHotel=function(hotel){
//undefiend
console.log(hotel);
}


Note:
On the first time post request and response is success ,but can't do after that

Answer

You are setting hotel to an array. You should set it to object instead.

$http({
      method:"POST",
      data:{'hotel':hotel},
      url:base_url+'operator/hotel/add_hotel'
  }).then(function serverResponse(response){
      //clear the old form data
      $scope.hotel={};   //this is what you should do
 });

Or set it to empty strings.

$scope.hotel.hotel_name = '';
$scope.hotel.hotel_loc = '';
Comments