在Bootstrap UI中使用$http
Using $http with Bootstrap UI
我正在开发一个使用AngularJS和Bootstrap的应用程序。我正在拼命地让我的自动完成示例发挥作用。当我的服务返回一个数组时,我的示例成功地工作了。然而,当我尝试使用$http服务访问REST API时,我无法再获得自动完成的结果。
我的Plunker在这里
根据这个功能在其他地方的使用方式,我需要坚持两件事:
1. The $http call must happen in the service.
2. The controller must call myService.getOptions.
有人能告诉我为什么我的代码不起作用吗。我太绝望了。
您需要让您的服务返回结果,因为在您的承诺中,它将返回整个响应,但您只需要提供结果,所以只需在服务中添加一个映射器即可。
this.getOptions = function(prefix) {
return $http.get('http://search.ams.usda.gov/farmersmarkets/v1/data.svc/zipSearch?zip=60629').then(function(response){
return response.data.results; //Return results
});
};
并在您的视图中进行一些更改以调整数据:-
typeahead="option as option.marketName for option in getOptions($viewValue)"
<h4><span bind-html-unsafe="match.model.marketname | typeaheadHighlight:query"></span></h4>
演示
或者在返回数据时使用服务中的映射器返回预期的数据格式:-
return $http.get('http://search.ams.usda.gov/farmersmarkets/v1/data.svc/zipSearch?zip=60629').then(function(response){
return (response.data.results || []).map(function(itm){return {id:itm.id, name:itm.marketname}}); //map your data
});
演示
相关文章:
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 浮动页脚栏-使用Bootstrap隐藏
- Meteor如何接收HTTP请求
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- 创建带有和不带有JavaScript的Bootstrap下拉菜单
- 在我的情况下,如何进行http请求
- //而不是在src=“”上使用http://"属性
- AngularJS/HTML/Bootstrap元素用于动态搜索结果
- Javascript+Bootstrap图像库未加载
- 我无法使用angularJs($http)访问服务器
- Bootstrap Dropdown selection是在*all*下拉菜单上设置选择
- AJAX简单错误.XMLHttpRequest无法加载http://localhost/mpl/getPage.php.
- 如何将PHP get查询转换为Meteor's HTTP.get()
- 角度异步http自动完成
- Bootstrap 3 Datepicker v4:dp.show和dp.change事件在Datepicker内联时不
- 在Bootstrap UI中使用$http
- 在angular-bootstrap-duallistbox中设置选中的项目,使用$http
- Angular-bootstrap使用$http提前输入
- 如何在本地主机上使用grunt、http-server运行bootstrap
- 如何添加onclick事件按钮在yii引导(http://www.cniska.net/yii-bootstrap/#t