0
如果我在ngFor循環中創建了選擇輸入,然後通過ngModel將值綁定到某個數組 - 這些選擇不會從它獲得它們的初始值數組(就像它只創建一個單向綁定)。下面是簡單的例子在plunker: https://plnkr.co/edit/LX7XyrHcMGH8avZYA0pv?p=previewAngular 2+ - ngModel不綁定初始值,以便在ngFor循環中進行選擇
@Component({
selector: 'my-app',
template: `
<div *ngFor="let select of test; let i = index" >
<select name="test" [(ngModel)]="vals[i]">
<option *ngFor="let item of select" [ngValue]="item">{{item.lol}}</option>
</select>
</div>
<div style="display: flex">
<div *ngFor="let val of vals; let last = last;">
{{val.lol}}{{last? '' : ','}}
</div>
</div>
`,
})
export class App {
test: any = [
[{ lol: '1' }, { lol: '2' }, { lol: '3' }],
[{ lol: '4' }, { lol: '5' }, { lol: '6' }],
[{ lol: '7' }, { lol: '8' }, { lol: '9' }],
];
vals: any[] = [{ lol: '1' }, { lol: '4' }, { lol: '7' }];
constructor() {}
}