如何使用最新的 $resource 填充 Angular UI Bootstrap Typeahead [英] How to populate Angular UI Bootstrap Typeahead with newest $resource
问题描述
根据 this Paweł Kozłowski 的回答,AngularUI-Bootstrap 的 Typeahead 在异步获取带有 $resource 的弹出项时应该可以工作最新的 Angular 版本(我使用的是 1.2.X).
According to this Paweł Kozłowski's answer, Typeahead from AngularUI-Bootstrap should work when asynchronously obtaining popup items with $resource in newest Angular versions (I'm using 1.2.X).
我想我不知道如何正确使用它(结果我在 typeaheadHighlight
指令的代码中收到错误 - typeahead 将立即返回的 Resource
s 视为字符串和轮胎来突出它们).
I guess I don't know how to use it properly (As a result I get an error in typeaheadHighlight
directive's code - typeahead treats instantly returned Resource
s as strings and tires to highlight them).
我认为关键代码是:
$scope.cities = function(prefix) {
var p = dataProviderService.lookup({q: prefix}).$promise;
return p.then(function(response){
$log.info('Got it!');
return response.data;
});
return p;
};
我尝试了很多东西 - 返回 $promise
(来自 Plunker 的版本)、query()
、then()
.
目前,我在我的应用程序中使用 $http
来实现此功能,我对此感到满意.不过,只是想知道如何使用 $resource
实现相同的效果.
I've tried bunch of stuff - returning $promise
(version from Plunker), query()
, then()
.
Currently, I'm using $http
for this functionality in my app and I'm ok with it. Still, just wanted to know how to achieve the same with $resource
.
你可能想看看这个:https://github.com/angular/angular.js/commit/05772e15fbecfdc63d4977e2e8839d8b95d6a92dui.bootstrap.typeahead
是否与 $resource 的 promise API 中的这些更改兼容?
You might want to take a look at this: https://github.com/angular/angular.js/commit/05772e15fbecfdc63d4977e2e8839d8b95d6a92d
is ui.bootstrap.typeahead
compatible with those changes in $resource's promise API ?
推荐答案
应该是:
$scope.cities = function(prefix) {
return dataProviderService.lookup({q: prefix}).$promise.then(
function(response){
// might want to store them somewhere to process after selection..
// $scope.cities = response.data;
return response.data;
});
};
这是基于上面提到的角度提交,它在 Angular 1.2.13 上对我有用
This is based of the angular commit mentioned above, and it worked for me on Angular 1.2.13
这篇关于如何使用最新的 $resource 填充 Angular UI Bootstrap Typeahead的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!