繁体   English   中英

mat-select 更改事件未以 setvalue angular 13 反应形式触发

[英]mat-select change event not trigger in setvalue angular 13 reactive form

在表单组内使用 mat-select

<mat-form-field appearance="outline">
   <mat-select formControlName="formula" id="formula">
       <mat-option [value]="metricFormula.TotalCount">{{l('TotalCount')}}</mat-option>
       <mat-option [value]="metricFormula.Sum">{{l('Sum')}}</mat-option>
       <mat-option [value]="metricFormula.Average">{{l('Average')}}</mat-option>
       <mat-option [value]="metricFormula.Percentage">{{l('Percentage')}}</mat-option>
    </mat-select>
</mat-form-field>

在 ts 文件中将值设置为表单控制formula ,不会触发更改事件

 get f() { return this.formGroup.controls; }

 private formBuild(): void {
        this.f.formula.setValue(metricFormula.Average);
}

更改事件代码不会被调用,但是,可以从列表中手动选择值

onChanges(): void {
    this.f.formula?.valueChanges.subscribe(val => {
        console.log(val);
    });
}

ngOnInit(): void {
        this.formBuild();
        this.onChanges();
    }

我必须实施AfterViewInit并为我工作

    export class MetricConfigureComponent implements OnInit, AfterViewInit {
      ngAfterViewInit() {
        setTimeout(() => {
           this.f.formula.setValue(//Some value of that type);
        });
      }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM