运行到无限循环消化,同时结合到具有$ HTTP内部功能 [英] Running into infinite digest cycle while binding to function that has $http inside
问题描述
我需要生成HTTP链接 A
标记。出于这个原因,我把 HTML
行
I need to generate http link for a
tag. For that reason I put in html
line
<a href="{{getLink(url)}}">My link</a>
在控制器它定义为:
$scope.getLink = function(inputUrl){
$http.get(inputUrl).success(function(data){/*.....*/});
}
为什么AngularJS无限循环结束了?什么是正确的设计?
Why AngularJS ends up in infinite cycle? What is the right design?
推荐答案
作为另一个答案解释,看了前pressions是在每一个消化和产生的价值进行评估时相比,他们的previous价值 - 脏检查。如果有变化,摘要的另一迭代开始,因为在一个值的变化可能会导致在另一变化
As explained in another answer, watched expressions are evaluated on every digest and the resulting value is compared to their previous value - dirty checking. If there is a change, another iteration of the digest starts because a change in one value might cause a change in another.
如果有一个循环依赖(包括一个圆,即相同的前pression是不同的每次),它会导致一个无限循环的角度经过10次迭代停止。
If there is a circular dependency (including, the circle of one, i.e. the same expression is different every time), it results in an infinite loop that Angular stops after 10 iterations.
具体来说,您的 getLink
函数的返回值是一种承诺,并在承诺角绑定不会等待(的$ HTTP返回值)。
Specifically, your getLink
function's return value is a promise (the return value of $http), and Angular bindings do not "wait" on a promise.
您想要做的是踢启动 $ HTTP
呼叫,并在其分配处理程序的返回值将被绑定到<$ C $一个ViewModel财产C>&LT; A&GT; :
What you want to do is to kick start the $http
call and in its handler assign the return value to a ViewModel property that would be bound to <a>
:
function getLink(){
$http.get(inputUrl)
.success(function(data){
$scope.url = data.data;
});
}
您可以致电 getLink
,例如,当你的控制器运行。
You can call getLink
, for example, when your controller runs.
在查看刚刚绑定网址
来的 NG-HREF
(未的href
)属性:
In the View you just bind url
to ng-href
(not href
) attribute:
<a ng-href="url">My Link</a>
这篇关于运行到无限循环消化,同时结合到具有$ HTTP内部功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!