Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
How can I call a function on another controller in a different directory from a controller is another directory where just copying it will not work?

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!