gpt4 book ai didi

Angular:在 String 上使用 ngSwitch 包括?

转载 作者:行者123 更新时间:2023-12-04 11:02:05 33 4
gpt4 key购买 nike

当产品描述搜索包含关键字时,我正在编写大量 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 示例:

我正在阅读 Sample Ngswitch 语句,但不知道如何转换上面的内容,如果它甚至可能是 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>

最佳答案

我刚读到一些关于 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>

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

关于Angular:在 String 上使用 ngSwitch 包括?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58742484/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com