Joe82 Joe82 - 6 months ago 18
AngularJS Question

AngularJS - Ng-route - Change ng-view when input:focus

I would like to change my view with ng-view. The point is that instead of firing an

ng-click
event, I would need to do it when somebody starts typing in an
input:text
field (so I could achieve the same effect as when start typing in the search box in this page).

PS: I tried adding
ng-click
to the input element, but that doesn't trigger the event. Any help would be much appreciated. Thanks!

Answer

All you need to do is to use ngChange directive with ngModelOptions (for debounce):

<input type="text" 
       ng-change="searchProduct()"
       ng-model="query" 
       ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 500, 'blur': 0 } }">

Then implementation of changeView is up to you, but I assume you would need to do something with $location.path to the change view:

$scope.searchProduct = function() {
  // ...
  $location.search({query: $scope.query});
};
Comments