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.
Asked
Active
Viewed 68 times
0

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 Answers
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(value){
console.log(value[2][0])
if(value[2][0] === "myController"){
myFactory.object={} // make the factory object null
}
})

Sachila Ranawaka
- 39,756
- 7
- 56
- 80
-
-
-
sorry, I don't understand how it must exactly works. Can you explain more detailed please? – kliukovking Aug 02 '17 at 12:24
-
`['_invokeQueue']` returns a array . so in order to access the element inside array we need to use a loop. that is why foreach loop is there – Sachila Ranawaka Aug 02 '17 at 12:25
-
I have a factory `app.factory('myFactory', function(){return{ obect:{} } })` I can't understand how I might use this checkup of working controllers :( – kliukovking Aug 02 '17 at 12:30
-
Why are you checking value[2][0] ? What is present at this indexes. Can you update your answer with more details? – Vivz Aug 02 '17 at 12:31