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);
};
答案 0 :(得分:0)
在homeresult()中,代码重定向到/ results。因此,如果您在定义md-autocomplete的同一页面上显示结果,而不是重定向,它将起作用。您可以添加一个简单的检查来隐藏结果,直到我们从$ http获得响应。