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

廉价感情. 提交于 2019-12-24 06:30:13

问题


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!

Edit: Here is the updated plunkr with the correct answer provided by dfsq


回答1:


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});
};


来源:https://stackoverflow.com/questions/37511072/angularjs-ng-route-change-ng-view-when-inputfocus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!