Define Active Controller

Is it possible to identify active controllers? With active, I do not mean loaded, but used in the current hierarchy of views.

What I want to do is update the global list of keyboard shortcuts, depending on which controllers are currently active (which keyboard shortcuts are available for the current view).

+7
angularjs
source share
2 answers

I think one way to do this is to use the service to store the name of the active controller or even the menu that you want to display. Each time you change the view, the controller will be executed, and you can run the function to update the service.

Here is a fiddle with a similar situation, I use the menu service, which stores different menus for each view, the service has setMenu () and getMenu (), each time the controller runs, it tells the service which menu should be activated, and any time I call getMenu (), it returns the active menu.

/* Services */ app.factory('Menu', function () { var activeMenu; var menu = { home: '<button>A</button><button>B</button>', list: '<button>C</button><button>D</button>', settings: '<button>E</button><button>F</button>', } function setMenu(name) { activeMenu = name; } function getActiveMenu() { return menu[activeMenu]; } return { setMenu: setMenu, getMenu : getActiveMenu } }); /* Controllers */ function HomeCtrl($scope, Menu) { Menu.setMenu('home'); $scope.menu = Menu.getMenu(); } function ListCtrl($scope, Menu) { Menu.setMenu('list'); $scope.menu = Menu.getMenu(); } function SettingsCtrl($scope, Menu) { Menu.setMenu('settings'); $scope.menu = Menu.getMenu(); } 
+5
source share

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) { // each time a scope is active this constructor will be called ActiveScopesServices.add(...); // each time a scope is removed this event receiver will be called $scope.$on('$destroy', function dismiss() { ActiveScopesServices.remove(...); }); 

});

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

+5
source share

All Articles