简体   繁体   English

我不明白为什么$ rootScope可以工作,但在这种情况下不能$ scope

[英]I don't understand why $rootScope works but not $scope in this case

I have an angular app, that places ng-app within the <html> tag instead of the body, so that the angular scope would cover the <title> . 我有一个角度应用程序,它将ng-app放在<html>标签而不是正文中,这样角度范围将覆盖<title> However, from within the angular controller, the $scope variable is not able to output anything within the <title> but $rootScope is. 但是,在角度控制器中, $scope变量无法在<title>内输出任何内容,而$rootScope可以。 I don't understand why this is the case. 我不明白为什么会这样。 As far as I know, $scope is local to the controller and $rootScope is global. 据我所知, $scope是控制器本地的, $rootScope是全局的。 However, in this case, I ensured that the ng-app was placed in the <html> tag so that <title> would be covered. 但是,在这种情况下,我确保将ng-app放在<html>标记中,以便覆盖<title> As far the ng-controller is concerned, I am not really specifying it anywhere within my html page. ng-controller而言,我并没有在html页面中的任何地方指定它。

<html ng-app="my-app">
<head>
    <meta charset="utf-8">
    <title>{{scopeVar}}</title>

Any hints? 有什么提示吗?

As pointed out by @deceeze, the reason why $rootScope worked but $scope didn't was simply because when working with ui-router , the controller is tied to the ui-view . 正如@deceeze指出的那样,$ rootScope起作用但$ scope不能起作用的原因仅仅是因为在使用ui-router ,控制器绑定到ui-view Hence, anything outside of that will not be covered by $scope . 因此, $scope不会覆盖任何超出此$scope But $rootScope on the other hand is global, and that covers anything within ng-app . 但另一方面, $rootScope是全局的,它涵盖了ng-app

<html ng-app="my-app" ng-controller="controller_name">
<head>
    <meta charset="utf-8">
    <title>{{scopeVar}}</title>
</head>
</html>

put your controller in <html> it will work with scope too.Because $scope is available in its controller and $rootScope is global to complete app. 将您的控制器放在<html>它也将与作用域一起工作。因为$ scope在其控制器中可用,而$ rootScope对于完成应用程序是全局的。

Grateful , when you add "ng-app" tag Angular gives you the $rootScope. 感激不尽 ,当您添加“ ng-app”标签时,Angular会为您提供$ rootScope。 When you create a controller Angular gives you a $scope. 当您创建控制器时,Angular会给您一个$ scope。

Prefer to create small controllers and put then where you want. 最好创建小型控制器,然后放置在所需位置。 When you create small controllers, you can use $scope for each one. 创建小型控制器时,可以对每个控制器使用$ scope。 Below i created two controllers, one to handler de header and other to handle my body. 在下面,我创建了两个控制器,一个用于处理抬头,另一个用于处理我的身体。 The " scopeVar " variable will be available in controller1 . scopeVar ”变量将在controller1中可用。

<html ng-app="my-app">
<head ng-controller="controller1">
    <meta charset="utf-8">
    <title>{{scopeVar}}</title>
</head>
<body>
    <div ng-controller="controller2">

    </div>
</body>
</html>

BUT, if you want to communicate from your controller to $rootScope, don't use $rootScope.somevariable = 'foo'. 但是,如果要从控制器与$ rootScope通信,请不要使用$ rootScope.somevariable ='foo'。 Use $broadcast and $emmit services to do that. 使用$ broadcast和$ emmit服务可以做到这一点。 Ex.: 例如:

$rootScope.$broadcast('event name', 'foo');

In your RUN configuration for the app you can listen that: 在应用程序的RUN配置中,您可以收听:

$rootScope.$on('event name', function(param1){
   // param1 will be 'foo'
   // do something
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM