首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角:对字符串包含使用ngSwitch吗?

角:对字符串包含使用ngSwitch吗?
EN

Stack Overflow用户
提问于 2019-11-07 05:29:55
回答 2查看 1.6K关注 0票数 3

当产品描述搜索包含关键字时,我正在编写大量的ngIf语句,呈现一个html表单。

如何将其转换为ngSwitch?

当前ngIf语句

代码语言:javascript
复制
<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的示例语句,但是不知道如何转换上面的语句,如果可能的话,在角度上是可能的。

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-07 05:52:45

我刚刚读到了一些关于ngSwitch条件的自定义技巧,您能这样做吗:

代码语言:javascript
复制
<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>

来源:https://stackoverflow.com/a/45950368/8597732

票数 2
EN

Stack Overflow用户

发布于 2019-11-07 05:42:44

为您的开关创建函数,用于可读的和干净的代码。

代码语言:javascript
复制
public  mySelection(selection: string): string {
    switch (selection) {
       case 'gf':
           return 'Gluten-free';
        default:
           return 'Standard';
    }
}

在html中

代码语言:javascript
复制
<div>
  <p >{{ mySelection('dietSelection') }}</p>
</span>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58742484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档