【问题标题】:Angular 2 ngModel change of select box not updating selected option选择框的Angular 2 ngModel更改未更新所选选项
【发布时间】: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,那么它会将最后一个选择框默认为蓝色,但仅限于第一个选择。之后的任何选择都与前一个相同。

https://plnkr.co/edit/Ze5uS1JjAmI7QXjQ17kQ

【问题讨论】:

    标签: angular select ngmodel


    【解决方案1】:

    将 2 路数据绑定 ([(ngModel)]) 与 (change) 事件一起使用是非常糟糕的主意,因为您无法预测/Angular 无法控制首先处理的操作。所以你必须重写你的entitySelect函数来手动为entity\a赋值。第二点有完全相同的原因......对我有用的例子:

    @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($event, a)" [ngModel]="a.selected">
                <option value="0">- Select -</option>
                <option *ngFor="let c of items" value="{{c}}">{{c}}</option>
            </select>
        </div>
    `,
    })
    export class App {
        name:string;
        items:Array<string> = ['red','green','blue'];
        constructor() {
            this.name = 'Angular2'
            this.arr = [{
                entities: [],
                selected: 0
            }]
        }
        entitySelect($event, entity) {
            entity.entities.push($event.target.value);
            $event.target.value = 0;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-14
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 2014-05-12
      • 2010-12-20
      • 2021-08-24
      相关资源
      最近更新 更多