Yes we can.
There are two resources on the Internet that will help us solve this problem:
So, as you can see, the question is impossible only if it: “does it make sense to know which controllers have an active scope”, because there is no sense in knowing anything about the controller’s life cycle itself.
First define RegisterableCtrl as follows (I did not check the code, just need to have an idea of how to achieve it)
myApp.controller("RegisterableCtrl", function ($scope, ActiveScopesServices) {
});
Then, for each controller in your application, enter the registration option, for example:
myApp.controller("MyCtrl2", function ($scope, ActiveScopesServices) { //inject registrable behaviour $injector.invoke(function ($controller) { $controller('RegisterableCtrl', {$scope: $scope}); //continue to normal code of your controller });
We can do better, because here we must call the $ injector at the beginning of each custom controller. Therefore, if you want to go further, look at what is called "AOP", aspect-oriented programming. There is already such an attempt:
https://github.com/mgechev/angular-aop
bdavidxyz
source share