AngularJS 中的 $$phase 是什么? [英] What is $$phase in AngularJS?

查看:20
本文介绍了AngularJS 中的 $$phase 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个代码片段是某人为引导模式编写的角度指令的一部分.

I found this code snippet which is part of a angular directive someone wrote for bootstrap modal.

//Update the visible value when the dialog is closed                                                                                                                                                                                                            
                //through UI actions (Ok, cancel, etc.)                                                                                                                                                                                                                         
                element.bind("hide.bs.modal", function () {                                                                                                                                                                                                                     
                    scope.modalVisible = false;                                                                                                                                                                                                                                 
                    if (!scope.$$phase && !scope.$root.$$phase)                                                                                                                                                                                                                 
                        scope.$apply();                                                                                                                                                                                                                                         
                });  

我理解这部分是我们绑定到hide.bs.modal事件和UI改变时更新modal的两种方式绑定的后半部分.

I understood that this part is for the latter half of two way binding we bind to hide.bs.modal event and update modal when UI changes.

我只是想知道为什么这个人在调用 apply 之前检查 $$phase 的作用域和 rootScope ?

I just wanted to know why is the person checking $$phase for scope and rootScope before calling apply ?

我们不能直接打电话申请吗?

Can't we straightaway call apply ?

这里的 $$phase 是什么?

What is $$phase here?

我尝试了很多搜索,找不到任何好的解释.

I tried searching a lot, couldn't find any good explanation.

我找到了我看到示例的地方:Bootstrap Modal 的简单 Angular 指令

推荐答案

$$phase 是一个标志集,而 angular 处于 $digest 循环中.

$$phase is a flag set while angular is in a $digest cycle.

有时(在极少数情况下),您希望在执行 $apply 之前检查范围上的 $$phase.如果您在 $digest 期间尝试 $apply 会发生错误:

Sometimes (in rare cases), you want to check $$phase on the scope before doing an $apply. An error occurs if you try to $apply during a $digest:

错误:$apply 已经在进行中

Error: $apply already in progress

这篇关于AngularJS 中的 $$phase 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆