[英]$scope and $rootscope variables' life term
In angularjs, I see a lot of usages for $scope and $rootscope. 在angularjs中,我看到$ scope和$ rootscope的很多用法。 I like to understand the life term of variables attached to $scope and $rootscope.
我想了解附加到$ scope和$ rootscope的变量的生命周期。 Say, I have a.js/a.html and e.js/e.html.
说,我有a.js / a.html和e.js / e.html。
a.js has $scope attached with variables say $scope.b, $scope.c, $scope.d for use in a.html. a.js的$ scope附加了变量,例如$ scope.b,$ scope.c,$ scope.d,用于a.html。 If I load another page with e.js and e.html.
如果我用e.js和e.html加载另一个页面。 Those $scope.b, $scope.c, $scope.d still have lives or they are removed.
那些$ scope.b,$ scope.c,$ scope.d仍有生命或被删除。 If I have $scope.b, $scope.c, $scope.d again in e.js, what could be issue?
如果我在e.js中再次使用$ scope.b,$ scope.c,$ scope.d,那么可能会出现什么问题? I just want to highlight the life term of those attached variables.
我只想强调那些附加变量的寿命期限。
$rootScope
- Usually configured in your main app.js file where you define your angular main module and main configuration. $rootScope
- 通常在主app.js文件中配置,您可以在其中定义角度主模块和主要配置。 Even if not configured, it can always be injected. $scope
- your current scope in the current controller (page). $scope
- 当前控制器(页面)中的当前范围。 $rootScope
variables live as long as you dont refresh your page. $rootScope
变量就会生效。 That means that they exist trough all transitions of your SPA. $scope
variables live only on the current scope (again, as long as you dont refresh the page) and they will get disposed once you transition to a different controller/page. $scope
变量仅存在于当前作用域上(同样,只要您不刷新页面),一旦转换到不同的控制器/页面,它们就会被处理掉。 $rootScope
lives as long as the whole page does $rootScope
只要整个页面都存在
$scope instances are destroyed whenever the controller or directive using it are no longer active 只要使用它的控制器或指令不再处于活动状态,就会销毁$ scope实例
No javascript is persistent between full page loads in browser 浏览器中的整页加载之间没有javascript持久化
$rootScope
is global while $scope
is local. $rootScope
是全局的,而$scope
是本地的。 When Controller is assigned to a page, so a $scope
variable can be use here because it binds to this controller. 当Controller被分配给一个页面时,因此可以在这里使用
$scope
变量,因为它绑定到该控制器。 But when we want to share its value across to other controllers or services, then $rootScope
is being used (**there are alternative ways share values across but in this case we want to use $rootScope
). 但是当我们想要将其价值分享给其他控制器或服务时,就会使用
$rootScope
(**有替代方法可以共享值,但在这种情况下我们想要使用$rootScope
)。
A property assigned with $scope
cannot be used outside the controller in which it is defined whereas a property assigned with $rootScope
can be used anywhere. 分配有
$scope
属性不能在定义它的控制器之外使用,而使用$rootScope
分配的属性可以在任何地方使用。
Which means when you want variable witch can be use in single variable to use in multiple controller (whole application), you can use $rootscope
这意味着当你想要变量时,可以在单个变量中使用多个控制器(整个应用程序),你可以使用
$rootscope
$rootScope
exists, but it can be used for evil$rootScope
存在,但它可以用于邪恶Scopes in Angular form a hierarchy, prototypally inheriting from a root scope at the top of the tree.
Angular中的作用域形成一个层次结构,原型继承自树顶部的根作用域。 Usually this can be ignored, since most views have a controller, and therefore a scope, of their own.
通常这可以忽略,因为大多数视图都有自己的控制器,因此也有范围。
Occasionally there are pieces of data that you want to make global to the whole app.
有时,您希望为整个应用程序提供全局数据。 For these, you can inject
$rootScope
and set values on it like any other scope.对于这些,您可以像任何其他范围一样注入
$rootScope
并在其上设置值。 Since the scopes inherit from the root scope, these values will be available to the expressions attached to directives likeng-show
just like values on your local$scope
.由于范围继承自根范围,因此这些值可用于附加到
ng-show
等指令的表达式,就像本地$scope
上的值一样。Of course, global state sucks and you should use
$rootScope
sparingly, like you would (hopefully) use with global variables in any language.当然,全局状态很糟糕,你应该谨慎使用
$rootScope
,就像你希望用任何语言的全局变量一样。 In particular, don't use it for code, only data.特别是,不要将它用于代码,只用于数据。 If you're tempted to put a function on
$rootScope
, it's almost always better to put it in a service that can be injected where it's needed, and more easily tested.如果您想在
$rootScope
上添加一个函数,那么将它放在一个可以在需要的地方注入的服务并且更容易测试几乎总是更好。Conversely, don't create a service whose only purpose in life is to store and return bits of data.
相反,不要创建一个服务,其唯一目的是存储和返回数据位。
— AngularJS Docs Miscellaneous - FAQ
- AngularJS Docs Miscellaneous - 常见问题解答
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.