- I've made a simple auto complete functionality on text search. There are two functions - querySearch(searchText) and homeresults(item). The querySearch function correctly fetches the results and there is no issue there. Once a result is selected by the user, then it calls homeresults function. This function works fine for the first time, but doesn't work after that unless I return to home page. So once a user is on results page, he can't use the search bar again unless he comes back to home page. There are no errors. Here is my code snippet:
HTML
<form ng-submit="$event.preventDefault()"> <md-autocomplete md-selected-item="selectedItem" md-search-text="searchText" md-items="item in querySearch(searchText)" md-item-text="item.title" md-delay="400" md-min-length="2" md-floating-label="Search for any Query"> <div layout-align="start center"> <span md-highlight-text="searchText" ng-click="homeresult(item)">{{item.title}}</span> </div> <md-not-found>No matches found </md-not-found> </md-autocomplete>
Javascript
$scope.querySearch = function(search) { var key = $scope.searchText; console.log('keyword searched by user is ' + key); return $http.get('/api/searchresult?title=' + key) .then(function(response) { return response.data }); }; $scope.homeresult = function(item) { console.log(item); $timeout(function() { item.title = item.title.replace(/ /g, '-'); $location.url('/results').search({ "title": item.title }) }, 50); };
Asked
Active
Viewed 145 times
1
1 Answers
0
In the homeresult(), code is redirecting to /results. So, instead of redirection, if you show results on the same page where md-autocomplete is defined, it will work. You can add a simple check to hide the results until we get response from $http.

Basavaraj Sonnad
- 215
- 1
- 7