1

Goal: I want a mat-dialog to show when the page is loaded.

Problem: I noticed that the ngOnInit is triggered twice when I have a mat-dialog in there.

What I've tried: I have tried moving it around to different methods but it seems to have no difference. Ive also looked through some SO articles but not sure I am understanding the answer. I see some mentioning the change detection.

Why is ngOnInit called twice?

Angular Material Dialog is displaying twice

Typescript:

ngOnInit()  
  {   
    
    sessionStorage.setItem('InActiveV', "false");

    // Start v in API and get  info    
    const staring = timer(500, 10000);
    this.startVSubscription = staring.subscribe(val =>{
      if (!this.isLoad) {
        this.PStartV();
      }
      else {
        this.startVSubscription.unsubscribe();
      }
    });
    this.cmdOpenDialog_click();
  }
  
  
  cmdOpenDialog_click() {
      this.dialog.open(DialogInternalNotesThreeComponent, {
          data: {
                data: this.internalNotes
            }
      });
  }
angleUr
  • 449
  • 1
  • 8
  • 27

1 Answers1

0

It could be due to a fault in your dialog (or a fault in any child element in the parent element), for example a missing closing tag in the HTML, or something simple like that. In the past I've noticed that if there's a fault in a child element, the ngOnInit() of my parent element is sometimes called twice.

  • One interesting thing is that this dialog can also open if someone clicks a specific button on the page. When they click that button the dialog only opens once. But for the page load its opening two of them. – angleUr Nov 09 '22 at 21:57