我有一個表,<tr>
與ngFor
循環。我只想顯示<tr>
,它與使用<select>
的雙向數據綁定屬性的值相匹配。Angular 2 ngFor Element不能用雙向數據綁定重新渲染
當應用程序第一次加載時,它工作正常,但當我更改select
選項視圖不會根據需要呈現。
__________CODE BELOW__________
HTML
<label>
Hours
<select
[(ngModel)]="location"
name="location">
<option *ngFor="let loc of locations" [value]="loc.id">{{loc.name}}</option>
</select>
</label>
<table>
<thead>
<tr>
<th>Location</th>
<th>Day</th>
<th>Open</th>
<th>Close</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let hour of hours">
<td *ngIf="hour.locationId === location">
locationId is {{hour.locationId}}
</td>
<td *ngIf="hour.locationId === location">
{{hour.day}}
</td>
<td *ngIf="hour.locationId === location">
{{hour.dayStart}}
</td>
<td *ngIf="hour.locationId === location">
{{hour.dayEnd}}
</td>
</tr>
</tbody>
</table>
組件
hours: any[] = [
{ locationId: 1, day: 'Sunday', dayValue: 0, dayStart: '9:00am', dayEnd: '7:00pm', open: false, working: false },
{ locationId: 1, day: 'Monday', dayValue: 1, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true },
{ locationId: 1, day: 'Tuesday', dayValue: 2, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true },
{ locationId: 1, day: 'Wednesday', dayValue: 3, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true },
{ locationId: 1, day: 'Thursday', dayValue: 4, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true },
{ locationId: 1, day: 'Friday', dayValue: 5, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true },
{ locationId: 1, day: 'Saturday', dayValue: 6, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true },
{ locationId: 2, day: 'Sunday', dayValue: 0, dayStart: '9:00am', dayEnd: '8:00pm', open: false, working: false },
{ locationId: 2, day: 'Monday', dayValue: 1, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true },
{ locationId: 2, day: 'Tuesday', dayValue: 2, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true },
{ locationId: 2, day: 'Wednesday', dayValue: 3, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true },
{ locationId: 2, day: 'Thursday', dayValue: 4, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true },
{ locationId: 2, day: 'Friday', dayValue: 5, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true },
{ locationId: 2, day: 'Saturday', dayValue: 6, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true }
],
locations: any[] = [
{ id: 1, name: 'Location 1', },
{ id: 2, name: 'Location 2', }
];
location: number = 1;
不知道'[值]'將數據存儲爲字符串,很高興知道 – locnguyen