MAT选择变化事件不是setValue Angular 13反应性形式的触发
在TS文件中使用form-group中的垫子选择
<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>
设置值以形式control 公式
,不会触发更改事件
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();
}
Using the mat-select inside the form-group
<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>
In the ts file setting the value to form control formula
, doesn't trigger the change event
get f() { return this.formGroup.controls; }
private formBuild(): void {
this.f.formula.setValue(metricFormula.Average);
}
Change event code doesn't get called, however, works on manually selecting the value from list
onChanges(): void {
this.f.formula?.valueChanges.subscribe(val => {
console.log(val);
});
}
ngOnInit(): void {
this.formBuild();
this.onChanges();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须实现
afterviewinit
并为我工作I have to implement
AfterViewInit
and worked for me