自动完成组件中的双向数据绑定

时间:2018-10-15 11:36:00

标签: angular angular-material angular6

我在同一页面上有int**个,即(车辆类型)new个我,有(车辆型号)如下图所示:

enter image description here

在这里,我想使用Filter autocomplete组件执行 2-way数据绑定

如果我选择Select with multiple selection作为 Cars ,那么Select with multiple selection的汽车就会像这样:

enter image description here

如果我选择Vehicle Type作为自行车,则自行车Vehicle Model将会像这样:

enter image description here

这是stackblitz链接。

1 个答案:

答案 0 :(得分:1)

FormControl有一个valueChages方法,该方法返回可观察到的并发出最新值的值。

尝试一下

ngOnInit() {
    this.list = this.CarList;
    this.filteredOptions = this.myControl.valueChanges
      .pipe(
        startWith(''),
        map(value => this._filter(value))
      );
    this.myControl.valueChanges.subscribe((d) => {

      if (d === 'Cars') {
        this.list = this.CarList;
      } else {
        this.list = this.BikeList;
      }
    })

示例:https://stackblitz.com/edit/angular-4kxeab-glx4vg

相关问题