当产品描述搜索包含关键字时,我正在编写大量的ngIf语句,呈现一个html表单。
如何将其转换为ngSwitch?
当前ngIf语句
<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>示例ngSwitch:
我正在阅读Ngswitch的示例语句,但是不知道如何转换上面的语句,如果可能的话,在角度上是可能的。
<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>发布于 2019-11-07 05:52:45
我刚刚读到了一些关于ngSwitch条件的自定义技巧,您能这样做吗:
<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>发布于 2019-11-07 05:42:44
为您的开关创建函数,用于可读的和干净的代码。
public mySelection(selection: string): string {
switch (selection) {
case 'gf':
return 'Gluten-free';
default:
return 'Standard';
}
}在html中
<div>
<p >{{ mySelection('dietSelection') }}</p>
</span>https://stackoverflow.com/questions/58742484
复制相似问题