Nicoleta Wilskon Nicoleta Wilskon - 2 months ago 7
AngularJS Question

Using ng-change in search box

Request :

$scope.viewAccount = function(){
var json = {

"json": {
"request": {
"servicetype": "60",
"functiontype": "1014",
"data": {
"shortname": $scope.model.selectShortName,
"groupzname": $scope.model.selectGname,
"city": $scope.model.selectCity,
"state": $scope.model.selectState,
"country": $scope.model.selectCountry,
"groupzcode": $scope.model.selectGcode,
"activationstatus": true,
"details": false,
"sortbasedon": $scope.groupzname,
"orderby": "desc",
"limit":10,
}

}
}
};

UserService.viewListAccount(json).then(function(response) {

console.log(JSON.stringify(json));
if (response.json.response.statuscode == 0 && response.json.response.statusmessage == 'Success')
{
$scope.tableData = response.json.response.data;
console.log($scope.tableData);
}
else{
alert(response.json.response.statusmessage);

}

});
};


The above function is to display all datas into table.I have a search box in groupzname,shortname and I used ng-change and called a new function changeviewAccount() removing offset in request. But the problem here I get is even if I enter one letter in searchbox, the function is getting called everytime. I am new to AngularjS .

Answer

Instead of ng-change , you can use ng-blur,whhich tells AngularJS what to do when an input box loses focus.

  <input ng-blur="changeviewAccount()" type="text" class="form-control"  placeholder="Title">