I am a ReactJS developer and have just entered the Angular4 club. I am using basic conditional statements with a super-simple condition just like this:
<div class="app-component">
<app-country *ngIf="condition"></app-country>
<app-country *ngIf="!condition"></app-country>
<div>condition is [{{condition}}]</div>
<button (click)="condition=!condition">Toggle condition</button>
</div>
This is my app.component.ts file. And the other app-country
is just ng g
created components and contains only <p> hello country</p>
. The condition in App.component.ts toggles every time. The problem is that the app-country
re-constructs all the time when condition triggers instead of re-render. Eg for the first time condition turns true from undefined then app-country
will get constructed and rendered. For the second time condition turns false and app-country
gets constructed and rendered. But it should get re-rendered the last constructed component.
I don't know is it an problem or this is the way Angular works. My point is does Angular has any way to resolve this in the way I wanted it to behave? Just like keys
in ReactJS that tells React that this the component key and React recognize that as an id of the component instance. Like this
<AppCountry key='app-country'/>
Any help will be appreciated. Thanks