javascript Angularjs 我可以从另一个控制器更改控制器的 $scope 值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28806321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angularjs Can I change $scope value of a controller from another controller?
提问by Arvind Kushwaha
I have an HTML div , like
我有一个 HTML div ,比如
<div id="loader-container" data-ng-controller="LoaderController" ng-show="shouldShow">
Some html design
</div>
And in my controller
在我的控制器中
angular.module('core').controller('LoaderController', ['$scope','$location', 'Authentication', '$rootScope',
function($scope,$location, Authentication, $rootScope) {
$scope.shouldShow = true;
}
]);
]);
And now, I want to hide that html div from another controller, That's why I tried to make $scope.shouldShow variable as false from another controller. So how can I make
现在,我想对另一个控制器隐藏该 html div,这就是为什么我试图将 $scope.shouldShow 变量从另一个控制器设为 false。那我该怎么做
$scope.shouldShow = false;
from another controller. Is it possible or any alternative way.?
来自另一个控制器。是否有可能或任何其他方式。?
回答by Vinay K
every controller will create a new Scope.
每个控制器都会创建一个新的范围。
Hence the changing the scope in one controller will not reflect in the scope of other controller.
因此,在一个控制器中更改范围不会反映在另一个控制器的范围中。
If you want to share a property across the controllers create that variable on the $rootScope
and use it.
如果要在控制器之间共享属性,请在 上创建该变量$rootScope
并使用它。
In Controller
在控制器中
$scope.$root.shouldShow = true; //or false
$scope.$root.shouldShow = true; //or false
In Markup
在标记中
<div ng-show="$root.shouldShow"></div>
<div ng-show="$root.shouldShow"></div>
回答by Faris Zacina
It's not a good idea to manipulate a $scope of a controller from a different controller directly. It would make your controllers very tightly coupled.
直接从不同的控制器操作控制器的 $scope 不是一个好主意。它会使您的控制器非常紧密地耦合。
One commonly accepted way to communicate between controllers is through messages using the publish-subscribe pattern.
在控制器之间进行通信的一种普遍接受的方式是通过使用发布-订阅模式的消息。
If your controllers are in completely separate components and you just need to notify the other controller, you can use the $broadcast/$emit
and $on
methods of the scope object to broadcast a message from one controller and listening to a specific message in a different controller. When an action happens that should change the shouldShow
variable, you can just broadcast a message and make the other controller listen and act on it.
如果您的控制器在完全独立的组件中并且您只需要通知另一个控制器,您可以使用范围对象的$broadcast/$emit
和$on
方法从一个控制器广播消息并在不同的控制器中监听特定消息。当发生应该更改shouldShow
变量的操作时,您可以广播一条消息并让其他控制器监听并对其采取行动。
Another common way to communicate between controllers is by using angular services, acting as a mediator between controllers.
控制器之间通信的另一种常见方式是使用角度服务,充当控制器之间的中介。
If your controllers are part of the same component/module, and you need to share state/behavior between those, then using an angular service
to encapsulate that logic and expose it would be an OK approach (services are singletons in Angular). That would be pretty simple to implement.
如果您的控制器是同一个组件/模块的一部分,并且您需要在它们之间共享状态/行为,那么使用 angularservice
来封装该逻辑并公开它是一种不错的方法(服务是 Angular 中的单例)。这将很容易实现。
However, not knowing more details about your exact requirements, it's hard to design a proper solution.
但是,如果不了解有关您的确切要求的更多详细信息,则很难设计出合适的解决方案。
Thanks to @Callum Linington for discussing the alternatives in the comments.
感谢@Callum Linington 在评论中讨论替代方案。