Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
AngularJS loads constructor multiple times

my controller is loaded multiple times in my AngularJS 1.5 code:

<div ng-repeat="conditionForMultipleRows"> 
 <div data-ng-if="$first">
   <div co-my-component></div>
 </div>
</div>
export function coMyComponent(): ng.IDirective {
  return {
    template: coMyComponentTemplateHtml,
    controller: 'MyComponentController',
    controllerAs:'$ctrl'
  }
}

export class MyComponentController{

state: MyStateClass;
static $inject = [someServices]
constructor(someServices) {
 document.getElementById("myComponent").addEventListener("myEvent", (ev: CustomEvent) => {
 doStuff()
}
    

The HTML Part is only called once, so there should be no issue. Only my controller is loaded multiple times. The angular.module only loads the controller once and the directive only once, so there is no issue. Also there is no other place where the controller or webcomponent is called in the code.

I'm not very familiar with AngularJS so you can also point out other parts if you see something wrong here. Please refer to a source if it was resolved there. I didnt find any helpful answer

Thanks in advance guys

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Each time your directive is instantiated, it will received a brand new controller.

The ng-repeat directive instantiates a template once per item from a collection. In your case, if conditionForMultipleRows is an array having four items inside, you will instantiate four times the template

<div data-ng-if="$first">
   <div co-my-component></div>
</div>

Each template instance gets its own scope and own controller, thus calling the constructor four times.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda