问题
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