角度异步http自动完成
Angular async http autocomplete
我想用Angular创建一个自动补全,因为这是我第一次接触Angular,所以我很困惑。
这是我的代码:
MenuService.getAutocompleteData(term).then(function (response) {
$scope.menuData = response.data;
});
这就是我如何调用正在进行以下http调用的服务:
return $http({
url : autocompletePath,
method : "POST",
data : {
term: term
}
}).success(function (response) {
return response;
});
问题是,这似乎是同步的,当我键入快速字母时,我的浏览器会冻结。我看到它是关于".然后"承诺,但我不知道如何解决它。任何帮助都将不胜感激。
您执行HTTP请求,这是正确的,但您尝试在成功时返回结果。相反,你应该尝试在你的成功处理程序中做需要做的事情。
所以在你的服务中,你会做这样的事情:
return $http({
url : autocompletePath,
method : "POST",
data : {
term: term
}
}); //note that there is no success handler in your service, because what's supposed to happen when the request completes, is the controller's business, not the service's. The service should only know where to get the data
你可以把你的控制器改成这样:
MenuService.getAutocompleteData(term).success(function (response) {
$scope.menuData = response.data;
}); //note the success-handler
为什么不试试呢?https://angular-ui.github.io/bootstrap/#/typeahead
相关文章:
- 角度异步http自动完成
- 如果需要登录,Angularjs$http拦截器会暂停异步请求
- 从 Ajax 获取由 ZombieJS 的异步点击事件触发的 Http 错误
- 如何在Angular中设置异步HTTP服务的回调,以便在不定义数组的情况下在控制器中填充数组
- 异步HTTP(ajax)请求在脚本标记中有效,但在js文件中无效
- 异步http请求服务
- 如何使用 ajax 或 jquery 读取异步 HTTP (servlet3.0) 响应
- 如何在 Node.js 中的 HTTP 请求后异步导出 JSON 数组
- AngularJS:异步 $http.get 调用导致 View 出现问题
- 如何异步加载 http://debug.build.phonegap.com
- 尝试使用 Angular $http通过表单异步提交文件
- 力量角器's on准备等待异步http请求
- SuiteScript;用户事件脚本中的异步HTTP请求
- 与$http和$resource异步$cacheFactory
- 在没有外部库的情况下,在Node中处理多个异步http请求的正确方法是什么?
- 在Node.js中对sqlite数据库进行异步http请求
- 如何使用$q异步返回http请求的值(使用typescript)
- 如何处理angularjs工厂服务中的$http异步问题?
- 在http post AngularJS异步中使用循环
- 我如何在Javascript中进行异步HTTP请求