【发布时间】:2016-09-27 12:28:17
【问题描述】:
我正在学习 Angular2。我正在尝试将数组值绑定到 UI。为此,我正在使用 ngFor 和 ngSwitch。我想根据ngSwitchCase显示记录。但现在它根据数组中的 id 显示记录。
HTML
<div *ngFor="let item of items; let i=index;">
<div [ngSwitch]="item.name">
<div class="form-control" *ngSwitchCase="'months_name'">
<label>First Field</label>
<div class="ctrl-wpr">
<md-input class="ctrl-wpr__ctrl" [(ngModel)]="item.value"></md-input>
</div>
</div>
<div class="form-control" *ngSwitchCase="'current_medication'">
<label>Second Field</label>
<div class="ctrl-wpr">
<md-input class="ctrl-wpr__ctrl" [(ngModel)]="item.value"></md-input>
</div>
</div>
<div class="form-control" *ngSwitchCase="'occupation'">
<label>Third Field</label>
<div class="ctrl-wpr">
<md-slide-toggle color="primary" [(ngModel)]="item.value"></md-slide-toggle>
</div>
</div>
<div class="form-control" *ngSwitchCase="'current_medical_problems'">
<label>First Field</label>
<div class="ctrl-wpr">
<md-input class="ctrl-wpr__ctrl" [(ngModel)]="item.value"></md-input>
</div>
</div>
<div class="form-control" *ngSwitchCase="'husband_medication'">
<label>Second Field</label>
<div class="ctrl-wpr">
<md-input class="ctrl-wpr__ctrl" [(ngModel)]="item.value"></md-input>
</div>
</div>
<div class="form-control" *ngSwitchCase="'spouseOccupation'">
<label>Third Field</label>
<div class="ctrl-wpr">
<md-slide-toggle color="primary" [(ngModel)]="item.value"></md-slide-toggle>
</div>
</div>
</div>
</div>
脚本
items: any = [{
"id": 2952,
"name": "months_name",
"value": "400201"
}, {
"id": 2964,
"name": "occupation",
"value": "Business"
}, {
"id": 7236,
"name": "spouseOccupation",
"value": "Housewife"
}, {
"id": 7244,
"name": "analysis_report",
"value": "11"
}, {
"id": 7245,
"name": "husband_medication",
"value": "No"
}, {
"id": 7246,
"name": "current_medication",
"value": ""
}, {
"id": 7247,
"name": "current_medical_problems",
"value": "Heart Problem",
}]
帮助我如何根据*ngSwitchCase显示/定位记录。
【问题讨论】:
标签: angular