如何在事件接收器中访问此角度 [英] how to access this inside an event receiver angular

查看:90
本文介绍了如何在事件接收器中访问此角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览本教程在我的angular 1.5应用程序中实现发送/接收广播消息.

I was going over this tutorial to implement sending/receiving broadcast messages in my angular 1.5 app.

所以在控制器内部我有这个:

so inside a controller I have this:

  update(){
    this.$rootScope.$broadcast('language_changed');
  }

在另一个控制器中,我得到了:

and in another controller I got this:

    $rootScope.$on('language_changed', function (event, data){
        this.updateStore();  <--- this here is undefined
    });

  }

  updateStore() {
      this.store = {
          ..
          language_id: this.LanguageService.get(),

        }
  }

我想知道如何在广播中掌握this. 此技巧不起作用

I'm wondering how can I get hold of this inside the broadcast. This trick didn't work

推荐答案

您提供的答案不是正确的解决方案,您需要了解您在做什么,制造黑客不是解决方案.请阅读以下文章,以了解this和$ scope this vs $ scope

The answer that you provided is not a proper solution, you need to understand what are you doing, making hacks is not a solution. Please read the following article to get a basic knowledge about the difference between this and $scope this vs $scope

重要的是还要了解角度事件:

Important to know aswell concerning angular events:

$ emit-将事件发送到范围的三个角度

$ broadcast-将事件发送到角度三下

此外,请查看以下代码段:代码段

Furthermore, please take a look at the following snippet: snippet

当您有两个控制器时,您无需同时从$ rootScope $广播事件并在$ rootScope上监听它,只需在$ scope级别上就可以监听,而$ rootScope依赖注入不是需要.查看以下内容:

When you have two controllers, you don't need to both $broadcast the event from $rootScope and listen to it $on $rootScope, it can be simply listened to on a $scope level, $rootScope dependency injection is not needed. Check out the following:

app.controller('DemoCtrl', function($scope, $rootScope) {
  $scope.broadCastEvent = function() {
    $rootScope.$broadcast('language_changed');
  };
});

假设我们要在用户单击按钮时广播事件.该事件沿范围的三个角度发送,因此正在监听此事件的控制器可以在范围级别上监听它,而无需$ rootScope依赖项注入,如下所示:

Let's say that we want to broadcast an event when user clicks a button. The event is sent down the angular three of scopes, so your controller that is listening to this event can listen it on scope level, without the $rootScope dependency injection, something like this:

app.controller('DemoCtrl2', function($scope) {
  $scope.$on('language_changed', function() {
   $scope.eventFired = !$scope.eventFired;
   });
})

另一种情况是,当您想要$ emit事件(将其发送到角度3)时,则需要将$ rootScope注入将要监听该事件的控制器,因为只有$ rootScope可以监听来自$ rootScope $ emit 的事件.查看以下内容:

The other case is when you want to $emit the event (send it up the angular three), then you would need to inject $rootScope to the controller that will be listening to that event, because only $rootScope can listen on a event that is being $emited from $rootScope. Check out the following:

控制器$ emiting事件:

Controller $emiting the event:

app.controller('DemoCtrl', function($scope, $rootScope) {
 $scope.emitEvent = function() {
  $rootScope.$emit('language_changed');
 };
});

控制器捕获事件:

app.controller('DemoCtrl2', function($scope, $rootScope) {
  $rootScope.$on('language_changed', function() {
   $scope.eventFired = !$scope.eventFired;
 });
})

尝试播放一下代码片段,看看要从$ rootScope $ emit发出事件并尝试在$ scope级别捕获事件,会发生什么情况.

Try playing with the snippet a bit, look what happends if you want to $emit the event from $rootScope and try to catch it on the $scope level.

希望这可以澄清一下.

这篇关于如何在事件接收器中访问此角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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