Issue
I am writing lot of ngIf statements, rendering an html form, when a product description search contains a keyword.
How do I convert this to ngSwitch?
Current ngIf statement
<app-store *ngIf="message?.productDescription.includes('Cars')">
<app-carform></app-carform>
</app-store>
<app-store *ngIf="message?.productDescription.includes('Reading')">
<app-bookform></app-bookform>
</app-store>
<app-store *ngIf="message?.productDescription.includes('Furniture')">
<app-homefurnitureform></app-homefurnitureform>
</app-store>
Example ngSwitch:
I am reading Sample Ngswitch statements, but don't know how to convert the above, if its even possible in angular.
<div [ngSwitch]="productDescription">
<p *ngSwitchCase="'Cars'"><app-carform></app-carform></p>
<p *ngSwitchCase="'Reading'"><app-bookform></app-bookform></p>
<p *ngSwitchCase="'Furniture'"><app-homefurnitureform></app-homefurnitureform></p>
</span>
Solution
I just read something about custom tricks of the ngSwitch condition, can you try something like that :
<div [ngSwitch]="true">
<app-carform *ngSwitchCase="message?.productDescription.includes('Cars')"></app-carform>
<app-bookform *ngSwitchCase="message?.productDescription.includes('Reading')"></app-bookform>
<app-homefurnitureform *ngSwitchCase="message?.productDescription.includes('Cars')"></app-homefurnitureform>
</div>
source: https://stackoverflow.com/a/45950368/8597732
Answered By - JStw
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.