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

192
Views
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 answers
Answer question

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 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!