[英]Conditional statement not working in Angular ngIf
I am working on sample project in which i am using GET request to fetch the data from API and storing it array.我正在处理示例项目,其中我使用 GET 请求从 API 获取数据并将其存储为数组。 And using that array i am creating radio buttons.并使用该数组创建单选按钮。
Example例子
Array大批
[
{
active: 0
categoryId: 8
categoryName: "ItemCustomerType"
codeName: "All"
description: "Type"
globalCodeId: 5
},
{
active: 0
categoryId: 8
categoryName: "ItemCustomerType"
codeName: "Agent"
description: "Type"
globalCodeId: 21
},
{
active: 0
categoryId: 8
categoryName: "ItemCustomerType"
codeName: "Private"
description: "Type"
globalCodeId: 22
}
]
Now i want to show only those radio buttons whose "codeName" value = Private or"codeName" value = Agent现在我只想显示那些“codeName”值 = Private 或“codeName”值 = Agent的单选按钮
Code for Radio Button单选按钮代码
<label class="radioBtn" *ngFor="let customerCodeName of itemSourceData;let i = index">{{customerCodeName.codeName}}
<input type="radio" value="{{customerCodeName.codeName}}"
(change)="onChangeItemSource(customerCodeName.codeName)" formControlName="itemType">
<span class="checkmark"></span>
</label>
**here itemSourceData contains the data fetched from API in form of array. **这里的 itemSourceData 包含从 API 以数组形式获取的数据。 But the PROBLEM is it is showing 3 radio buttons for codeName:'All','Agent','Private' but it should show only 2 radio buttons for 'Agent' and 'Private' only **.但问题是它显示了 3 个代码名称的单选按钮:'All','Agent','Private',但它应该只显示 2 个单选按钮,用于'Agent' 和 'Private' **。
Any solution please?请问有什么解决办法吗?
Filter your list for your loop.为您的循环过滤您的列表。
let list = itemSourceData.filter(i=>i.codeName==='Private'||i.codeName==='Agent');
Please add an if statement in your input type to only add radio buttons if it is Private
or Agent
inside for loop.请在您的输入类型中添加一个 if 语句,以仅在 for 循环中为Private
或Agent
时添加单选按钮。
HTML HTML
<label class="radioBtn" *ngFor="let customerCodeName of itemSourceData; let i = index"
>{{ customerCodeName.codeName }}
<input
*ngIf="
customerCodeName.codeName === 'Private' || customerCodeName.codeName === 'Agent'
"
type="radio"
value="{{ customerCodeName.codeName }}"
(change)="onChangeItemSource(customerCodeName.codeName)"
formControlName="itemType"
/>
<span class="checkmark"></span>
</label>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.