0

Is there any way to get list of working controllers at the moment? I have an object in my factory and also have controller, for example "myController". So I need to make myFactory.object={}(make this object empty) if this controller is disconnected from the module.

Vivz
  • 6,625
  • 2
  • 17
  • 33
kliukovking
  • 569
  • 1
  • 5
  • 16
  • $state.current.controller . How about this? –  Aug 02 '17 at 12:12
  • Possible duplicate of https://stackoverflow.com/questions/44153816/get-current-controller-in-use-in-angularjs –  Aug 02 '17 at 12:13
  • Check out this question. It might help. https://stackoverflow.com/questions/23382734/get-controller-name-from-scope – Chirag Aug 02 '17 at 12:15
  • It's not possible - some controllers don't even have names. Not a problem, since this is XY problem. – Estus Flask Aug 02 '17 at 12:39

2 Answers2

2

You might use $destroy event which is bound on $scope of your controller to notify the factory.

An option could be something like this:

.controller('MyController', ['$scope', 'MyFactory', function($scope, MyFactory) {

  $scope.$on('$destroy', function() {
    MyFactory.object = {};
  })

}])
korteee
  • 2,640
  • 2
  • 18
  • 24
  • and if I use controller as construction instead of $scope I can write `this.$on('destroy....`, right? – kliukovking Aug 02 '17 at 12:32
  • 1
    @RoGGeR No. `this` isn't scope. There's no $on method, unless you defined it. $scope is supposed to be used in controllers. The preferable way is [$onDestroy](https://docs.angularjs.org/guide/component) hook. – Estus Flask Aug 02 '17 at 12:37
0

in your factory use _invokeQueue to get the registered controllers, provides etc

angular.module('myModule')['_invokeQueue'].forEach(function(valu‌e){ 
      console.log(value[2][0]) 
      if(value[2][0] === "myController"){
          myFactory.object={} // make the factory object null 
      } 
})
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80