Tuesday, 14 March 2023

angular ngOnint twice

 https://stackoverflow.com/questions/38787795/why-is-ngoninit-called-twice

1)

Putting this here in case someone wind up here. NgOnInit can also be called twice if your browser's default button type is "submit", say if you have the below, NgOnInit of NextComponent will be called twice in Chrome:
<button class="btn btn-primary" (click)="navigateToNextComponent()">

To fix it, set type:

<button class="btn btn-primary" type="button" (click)="navigateToNextComponent()">


2)

This happened to me in a child component. The child component is to show when a certain condition is met using *ngIf. My solution is to replace ngIf with ngClass.

in HTML:

<component [ngClass]="isLoading?'hide':'show'>"

in CSS:

.show{ display: block; }

.hide{ display: none

No comments:

Post a Comment