5

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>
8
  • As per this answer, you can't. Another alternative would be to move the if logic to determine what to be displayed in the component and then you can just call that method when you need it to be displayed on the template. Commented Nov 7, 2019 at 5:49
  • 1
    It would work but its just a hack. IMO, it would be better to move the if-logic to the component and just call that method from the template (html) Commented Nov 7, 2019 at 5:55
  • hi @NicholasK or just simple answer in stack paragraph, either is good, appreciate it! Commented Nov 7, 2019 at 5:57
  • I guess it will help you stackoverflow.com/questions/43423458/… Commented Nov 7, 2019 at 5:58
  • Just observed that you are calling other components based on your if-blocks. In that case, the best you can do is to extract this portion message?.productDescription.includes('Cars') into a method in your component and then invoke that method by passing in a parameter. Not sure if that would work for you. Commented Nov 7, 2019 at 6:07

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

Comments

0

create function for your switch, for readable and clean code.

public  mySelection(selection: string): string {
    switch (selection) {
       case 'gf':
           return 'Gluten-free';
        default:
           return 'Standard';
    }
}

In your html

<div>
  <p >{{ mySelection('dietSelection') }}</p>
</span>

1 Comment

hi @Poldo, well I need to display application components, you'res is only displaying values, which will not be sufficient for what I'm trying to do, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.