1
  1. 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:
  2. 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>
    

  3. 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);
     };
    
Vivz
  • 6,625
  • 2
  • 17
  • 33
Gurdeep
  • 11
  • 1
  • 1

1 Answers1

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.