In some cases, System has to provide suggestions to user while user typing. Sending request for each and every request to server will be overhead. So sending request to server when user finishes typing will give good performance. We can detect whether user typing or finished his typing by delaying the ng-change event.

Markup

Call function with ng-change. displayName function will be triggered when user changes input
<input ng-change="displayName()" ng-model="name"/>
<div ng-bind="name1"></div>

Script

Use $timeout to delay the function
function myCtrl($scope, $timeout) {
     $scope._timeout  = null;
     $scope.name = 'sri';
     $scope.FilterByName = function () {
        if($scope._timeout){ //if there is already a timeout in process cancel it
          $timeout.cancel($scope._timeout);
        }
        $scope._timeout = $timeout(function(){
           $scope._timeout = null;
           $scope.name1 = $scope.name;
        },1000);
      };
}

0 comments:

Blogroll

Popular Posts