迫使角摘要 [英] Forcing a digest in Angular
问题描述
在用户操作后,某些时候,我想引起消化发生的,所以UI反映了数据模型支持它的变化。
At some point after a user action I would like to cause a digest to occur, so the UI reflects a change in the data-model backing it.
我有一个回调(异步)
据我所知, $范围
才有意义,在控制器的情况下。我可以通过执行实现同样的效果 $适用()
在 $ rootScope
?
I understand that $scope
only makes sense in the context of a controller. Can I achieve the same effect by performing $apply()
on the $rootScope
?
我见过code,用来检查 $$阶段
或类似的相关避免摘要错误,哪些检查,我应该为了安全地触发摘要执行?
I have seen code that checks for $$phase
or similar related to avoiding digest errors, what checks should I perform in order to trigger a digest safely?
推荐答案
看到这个答案:<一href=\"http://stackoverflow.com/questions/17868889/running-apply-on-rootscope-vs-any-other-scope\">Running在$ rootScope $适用VS任何其他范围
您可以致电 $ rootScope。$适用()
控制器之外(即服务),以引发消化循环。
You can call $rootScope.$apply()
outside of a controller (i.e. in a service) in order to trigger a digest loop.
另外,你可以考虑使用 $广播
和 $关于
来的通知发送到您的应用程序的其它部分当事情需要刷新。 (请参见理解角的$范围和$ rootScope事件系统$冒出, $广播和$上)
Alternatively, you could consider using $broadcast
and $on
to send a notification to other parts of your app when something needs refreshing. (See Understanding Angular’s $scope and $rootScope event system $emit, $broadcast and $on)
// in a service
$rootScope.$broadcast('myCustomEvent', {
someProp: 'foobar'
// other data
});
// in a controller or another service
$scope.$on('myCustomEvent', function (event, data) {
console.log(data);
// do something with this event
});
这篇关于迫使角摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!