I'm in the middle of migrating code from Angular 1.3 to Angular 1.5 components and ES6 controllers. 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
.module('test')
.directive('test', function() {
return {
link: function(scope) {
scope.$on('$stateChangeStart', function(event, toState, toParams) {
//logic goes here..
});
}
}
});
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. 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!