angularjs:异步调用后视图数据不会更新 [英] angularjs: view data doesn't update after async call
问题描述
我正在使用第三方js库向其api发出http请求.我使用"ng-keyup"对输入字段中的每个击键执行此操作.在对api的调用返回之后,我设置了$ scope.results来填充视图.但是视图总是在后面更新一次迭代,调试看起来好像是通过异步调用进行的.
I am using a 3rd party js library to make http requests to their api. I perform this operation on every keystroke in an input field using "ng-keyup". After the call to there api returns I set the $scope.results which populates the view. However the view always updates one iteration behind, debugging this looks to be with the async call.
有没有一种方法可以刷新视图或以不同的方式进行异步调用,而不仅仅是回调?
Is there a way to refresh the view or make the async call differently rather than just a callback?
推荐答案
看起来您的回调可能在角度上下文之外执行.尝试在异步回调中调用 $ scope.$ apply()
.如果这不是问题,请发布一些代码:)
Looks like your callback might be executing outside of angular context. Try calling $scope.$apply()
in async callback. If that's not the issue, post some code please :)
这篇关于angularjs:异步调用后视图数据不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!