[英]change scope value in already loaded controller from another controller without rootScope
I need to update one $scope value of controller-2(already loaded) from controller-1. 我需要从controller-1更新一个controller-2(已加载)的$ scope值。 I don't want to use $rootScope. 我不想使用$ rootScope。
Can I use broadcast or emit for same? 我可以使用广播或发射吗?
If the controllers are in a child-parent relationship, you can use: 如果控制器之间有子代关系,则可以使用:
If they are not in this relationship, then you need to: 如果他们不在这种关系中,那么您需要:
Of course... this is not always the best way to go. 当然...这并不总是最好的方法。
If you prefer, you can use other ways to communicate (eg use a common service). 如果您愿意,可以使用其他方式进行通信(例如,使用通用服务)。
Use brodcast on rootscope and listen on scope. 在rootscope上使用brodcast并在范围上侦听。
The best practice is to create a service for each custom event you want to listen to or broadcast. 最佳做法是为您要收听或广播的每个自定义事件创建服务。
.service("hiEventService",function($rootScope,$scope) {
this.broadcast = function() {$rootScope.$broadcast("hi")}
this.listen = function(callback) {$scope.$on("hi",callback)}
}) })
You can use $localStorage for change that value. 您可以使用$ localStorage更改该值。 or create one service for get and set that value. 或创建一个获取服务并设置该值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.