[英]How to show results from remote query in Angular-material autocomplete?
I'm writing a website using angular-material in which I use the auto-complete . 我正在使用角度材料编写一个网站,我使用自动完成 。 Within this autocomplete ( codepen here ) there is a function which returns the searchresults from a local array of demo search-items which (simplified) looks like this:
在这个自动完成( 这里是codepen )中有一个函数,它从一个本地的demo搜索项数组中返回搜索结果,其中(简化)如下所示:
function querySearch (query) {
var results = query ? self.repos.filter( createFilterFor(query) ) : self.repos, deferred;
return results;
}
I now want to edit this code so that I can query the server after typing each letter. 我现在想要编辑此代码,以便在键入每个字母后可以查询服务器。 So I wrote the angular code to query the server and return a promise, as instructed here :
所以我写了角度代码来查询服务器并返回一个promise, 如下所示 :
function querySearch (query) {
return $http.get('/ajax/user/search/' + query).then(function(data){
console.log(data); // I've got the data here. All fine.
return data.data;
});
}
Unfortunately this doesn't really work; 不幸的是,这并没有真正奏效; the server gets queried fine, but I see an empty list of suggestions as you can see below:
服务器被查询正常,但我看到一个空的建议列表,如下所示:
Does anybody know how I can solve this issue? 有谁知道如何解决这个问题? All tips are welcome!
欢迎所有提示!
Check out the example here and click off simulate query and disable caching of queries - https://material.angularjs.org/latest/#/demo/material.components.autocomplete 查看此处的示例并单击关闭模拟查询并禁用查询缓存 - https://material.angularjs.org/latest/#/demo/material.components.autocomplete
Just make sure you disable cache md-no-cache="false"
if you truly want it to fire every time and set md-min-length="0"
如果您确实希望每次都触发并设置
md-min-length="0"
确保禁用缓存md-no-cache="false"
md-min-length="0"
You just want to replace the timeout they have to mock the query with your actual query 您只想用实际查询替换模拟查询所需的超时
function querySearch (query) {
var results = query ? self.states.filter( createFilterFor(query) ) : self.states,
deferred;
if (self.simulateQuery) {
deferred = $q.defer();
//repalce this
$timeout(function () { deferred.resolve( results ); }, Math.random() * 1000, false);
return deferred.promise;
} else {
return results;
}
}
And return your promise accordingly. 并相应地回复你的承诺。
One quickfix would be to just return the function as: 一个quickfix就是将函数返回为:
function querySearch (query) {
return $http.get('/ajax/user/search/' + query).success(function(data){
console.log(data); // I've got the data here. All fine.
return data.data;
});
}
and then in the view change the call to: 然后在视图中将调用更改为:
md-items="item in ctrl.querySearch(ctrl.searchText)()"
therefore executing the function you return now. 因此执行你现在返回的功能。
(Codepen to showcase the solution: http://codepen.io/anon/pen/qdyEBy ) (Codepen展示解决方案: http ://codepen.io/anon/pen/qdyEBy)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.