MAT选择变化事件不是setValue Angular 13反应性形式的触发

发布于 2025-02-13 03:38:47 字数 1036 浏览 0 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2025-02-20 03:38:47

我必须实现afterviewinit并为我工作

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

I have to implement AfterViewInit and worked for me

    export class MetricConfigureComponent implements OnInit, AfterViewInit {
      ngAfterViewInit() {
        setTimeout(() => {
           this.f.formula.setValue(//Some value of that type);
        });
      }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文