So, I know there are other solutions here... but my situation is kind of unique.
The first controller, and yes, this is in AngularJS for a huge telecom, has several unique $scope variables which cannot be moved or copied. This code is a MESS!
So, I've tried:
$rootScope.CallMainFunction, (incomingData) => {
$scope.mainFunction(incomingData);
};
and the mainFunction is here... all complex but I've dumbed it down
$scope.mainFunction = (data) => {
--- Do lots of stuff here with lots of variables NOT available anywhere else
}
The calling function:
$rootScope.$emit.CallMainFunction(id);
The two controllers are separated by two folders:
--- MODULES (just the name of the directory)
---| folder 1
---| folder 2
---| folder 3
---| folder 50 <-- YEAH there are this many.
I COULD make a service but this is such a dysfunctional app, it'll take weeks. That's the CORRECT way to do it "IF" I was in Angular 6 - 14.
So, I'm left with trying to call a function to a function within another controller. Thoughts please?
Assuming the 'sending' controller is a parent of the 'receiving' controller (being that it's sent via $rootScope), you can broadcast the event down and then execute the function in it's own controller.
//broadcast the event down
$rootScope.CallMainFunction = function (someData) {
$scope.$broadcast("callMainFunction", someData);
}
Then in the receiving controller, listen for the event. Also, don't forget to kill the listener when the controller gets unloaded
//handle SendDown event
var myListener = $scope.$on("callMainFunction", function (evt, data) {
$scope.theMainFunction(data)
});
$scope.$on('$destroy', myListener);