为$ rootScope最上面的$范围的家长吗? [英] Is $rootScope the parent of the topmost $scope?

查看:87
本文介绍了为$ rootScope最上面的$范围的家长吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个共享的函数返回我的AngularJS应用程序的最顶层元素(文件)的范围。

I have a shared function which returns of the scope of the topmost element (document) in my AngularJS application.

function topScope() { 
  return angular.element(document).scope();
}

这始终工作,我一直保证可以访问位于应用程序中(无论是内部控制器或指令)的任何subscop​​es。

This always works and I am always guaranteed to have access to any subscopes located within the application (whether it be inside controllers or directives).

下面是什么,我会用它为例子:

Here's an example of what I would use it for:

topScope().$emit('pageReady');

现在我已经注意到,$ rootScope也以同样的方式。

Now I've noticed that $rootScope also works the same way.

$rootScope.$emit('pageReady');

也可以工作并达到相同的效果。但由于$ rootScope的设计是$范围现成的范围,那么这是否仍然意味着它实际上是在页面的最上面范围(创建将继承它的方法和属性的范围)?因此被附加到文档节点的范围对象的父

Which also works and achieves the same affect. But since $rootScope is designed to be the "$scope off the shelf" scope (any scope created will inherit it's methods and properties) then does this still mean that it is in fact the topmost scope of the page? Thus being the parent of the scope object attached to the document node?

推荐答案

$ rootScope 是所有范围内的在一个给定的AngularJS应用父范围。由于能够在一页上引导多个AngularJS应用中,也可能有多个<$ C(仅手动,这不能使用纳克应用完成) $ C> $ rootScope 在一个HTML文档实例。

$rootScope is a parent scope of all scopes in a given AngularJS application. Since it is possible to bootstrap multiple AngularJS applications on one page (only manually, this can't be done using ng-app) it is also possible to have multiple $rootScope instances in one HTML documents.

每个 $ rootScope 被附加要么在那里ngApp被宣布为元素或传入的angular.bootstrap 这里描述

Each $rootScope is "attached" to either the element where ngApp was declared or the element passed into angular.bootstrap as described here.

总之, $ rootScope 是一个AngularJS应用程序的所有范围的根,但没有超级根的范围,以此作为一个父范围所有其他范围对于给定的HTML文档。

In short, the $rootScope is a root of all scopes for one AngularJS application but there is no "super-root" scope that would serve as a parent scope of all other scopes for a given HTML document.

在您的情况下使用 $ rootScope 如果你已经得到了整个HTML文档中只有一个AngularJS应用程序可能会确定。

In your case using the $rootScope might be OK if you've got only one AngularJS application in the whole HTML document.

这篇关于为$ rootScope最上面的$范围的家长吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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