Search input and $state.go

强颜欢笑 提交于 2019-12-08 02:45:15

问题


I've built a small movie search app using AngularJS and Elasticsearch.
I used AngularJS UI Bootstrap Typeahead for autocomplete.

My app is pretty simple, hp, results page, contact form.
What I'd like to do is use ngKeydown/ngKeypress and $state.go on the search input.
What I'm looking for is when a user starts typing their query on the hp in the search input, the page goes to the results page on the first keypress and then suggestions begin to appear.

Something like this:

<form>
  <input type="text ng-keypress="$state.go('results')">
</form>

回答1:


The dependencies of your controller aren't magically available in your html. You need to create the function on the controller, and call that function from your html.

controller:

function myController($scope, $state) {
    $scope.navigate = function (destination) {
        $state.go(destination);
    }
}

html:

<form>
    <input type="text ng-keypress="navigate('results')">
</form>



回答2:


You need to inject $state service in controller. You can use only $scope or $rootScope object in html. So you need to call some function on key-press or key-down and change state from controller.



来源:https://stackoverflow.com/questions/40012048/search-input-and-state-go

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