简体   繁体   English

如何在Angular 1.5组件中监听范围事件?

[英]How to listen to scope events in Angular 1.5 component?

I'm in the middle of migrating code from Angular 1.3 to Angular 1.5 components and ES6 controllers. 我正在将代码从Angular 1.3迁移到Angular 1.5组件和ES6控制器。 I tried to find something here on SO, but not helpful enough. 我试图在这里找到一些东西,但没有足够的帮助。 Suggestions required on how to watch events on scope other than the way mentioned below. 除了下面提到的方式之外,如何在范围内观看事件所需的建议。 Or how to trigger scope events from the directive. 或者如何从指令触发范围事件。 Also please suggest the correct way of doing so, if an alternative exists. 如果存在替代方案,也请建议正确的方法。

Angular 1.3 Angular 1.3

angular
.module('test')
.directive('test', function() {
    return {
        link: function(scope) {
            scope.$on('$stateChangeStart', function(event, toState, toParams) {
                //logic goes here..
            });
        }
    }
});

Angular 1.5/ ES6 Angular 1.5 / ES6

class TestController {
    /* @ngInject */
    constructor($scope) {
        this._$scope = $scope;
    }

    $onInit() {
        this._$scope.$on('$stateChangeStart', (event, toState, toParams) => {
            //logic goes here
        });
    }
}

angular
.module('test')
.component('test', {
    controller: TestController
});

Edit: 编辑:

Interested in an alternative for $on and not $watch here, cause $onChange can replace $watch when you are just watching variables. 有兴趣选择$ on而不是$ watch,因为$ onChange可以在你看变量时替换$ watch。 I want to listen to scope events, as not 100% of the angular 1.3 code can be migrated to 1.5, I still have directives triggering events on scope! 我想听范围事件,因为不是100%的angular 1.3代码可以迁移到1.5,我仍然有指令触发范围上的事件!

Scope events can be converted to RX observables in a service. 范围事件可以转换为服务中的RX可观察对象。

 app.factory("rxLocationChangeStart", function($rootScope, rx) {
     var rxLocationChangeStart = new rx.Subject();
     $rootScope.$on("$locationChangeStart", function() {
       rxLocationChangeStart.onNext(arguments);
     });
     return rxLocationChangeStart;
 })

Then a component can subscribe to those events: 然后组件可以订阅这些事件:

 app.component("locationMonitor", {
       scope: {},
       template: ['<p>oldPath={{$ctrl.oldPath}}</p>',
                  '<p>newPath={{$ctrl.newPath}}</p>'].join(''),
       controller: function (rxLocationChangeStart) {
         var $ctrl = this;
         var subscr = rxLocationChangeStart.subscribe(function(data) {
             console.log("locationChangeStart ", data);
             $ctrl.newPath = data[1];
             $ctrl.oldPath = data[2];
         });
         this.$onDestroy = function() {
           subscr.dispose();
         };
       }
 })

Angular 2 replaces the scope event bus with RX Observables. Angular 2用RX Observables替换范围事件总线。 Converting scope events to RX Observables provides an easy migration path from AngularJS to Angular 2. 将范围事件转换为RX Observables提供了从AngularJS到Angular 2的简单迁移路径。

The DEMO on PLNKR . PLNKR上DEMO

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

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