【发布时间】:2017-05-01 13:04:42
【问题描述】:
I may or may not have found a bug in Angular 2. Basically what I'm trying to do is create a list of selected items of options chosen from a select box and when an item is selected it creates a new empty select前一个下方的框,以便用户可以连续添加所选项目。
所以我想要做的是将底部选择框重置为空值,但是当我尝试将 ngModel 值设置回 0(或空)时,它仍然将底部选择框保留在先前选择的选项中。
@Component({
selector: 'my-app',
template: `
<div *ngFor="let a of arr">
<div *ngFor="let b of a.entities;let i = index">
<select class="form-control input-sm" [(ngModel)]="a.entities[i]">
<option *ngFor="let c of items" value="{{c}}">{{c}}</option>
</select>
</div>
<select class="form-control input-sm mb5" (change)="entitySelect(a)" [(ngModel)]="a.selected">
<option value="0">- Select -</option>
<option *ngFor="let c of items" value="{{c}}">{{c}}</option>
</select>
</div>
`,
})
export class App {
items:Array<string> = ['red','green','blue'];
constructor() {
this.arr = [{
entities: [],
selected: 0
}]
}
entitySelect(entity) {
entity.entities.push(entity.selected);
entity.selected = 0; // Revert bottom select box back to empty
}
}
https://plnkr.co/edit/PMzbgEtyd4DFhObu1UVz
另一个奇怪的事情是,如果我将 entity.selected 设置为“蓝色”而不是 0,那么它会将最后一个选择框默认为蓝色,但仅限于第一个选择。之后的任何选择都与前一个相同。
【问题讨论】: