Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda