【问题标题】:Angular Option groups autocomplete set default valueAngular 选项组自动完成设置默认值
【发布时间】:2020-07-07 10:15:24
【问题描述】:

如何为以下选项组自动完成示例设置默认值。

https://stackblitz.com/angular/plrolpkqnag?file=src%2Fapp%2Fautocomplete-optgroup-example.ts

【问题讨论】:

标签: angular angular-material


【解决方案1】:

如果要设置默认值,可以在 ngOnInit() 中使用此代码

     this.stateForm.patchValue({
         stateGroup: "Alabama",
     });

我的意思是ngOnInit() 必须像下面这样:

    ngOnInit() {
        this.stateGroupOptions = this.stateForm.get('stateGroup')!.valueChanges
            .pipe(
                startWith(''),
                map(value => this._filterGroup(value))
            );

        this.stateForm.patchValue({
            stateGroup: "Alabama",
        });
    }

或者你可以动态地:

    this.stateForm.patchValue({
        stateGroup: this.stateGroups[0].names[0],
    });

【讨论】:

  • 是的,实际上你的问题不是 MatAutocompleteModule,它只是用于输入
猜你喜欢
  • 2017-07-24
  • 2020-10-14
  • 2020-12-08
  • 2021-10-25
  • 1970-01-01
  • 2017-02-05
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
相关资源
最近更新 更多