Angular选项组自动完成设置默认值

时间:2020-07-06 12:42:03

标签: angular angular-material

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

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

1 个答案:

答案 0 :(得分: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],
    });