且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular 5-如何在表单值更改上添加条件字段

更新时间:2023-02-12 10:34:04

尝试将代码更改为以下内容:

Try changing the code to something like this:

this.form.valueChanges
.subscribe(values => {
    if(value['require_formatting'] === 'yes' && this.form.get('formatting_type')) {
        this.form.addControl('formatting_type', myFormControl); // Add new form control
    }
});

应该只运行一次if语句中的代码,以消除堆栈溢出.

It should get rid of the stack overflow by only running the code in the if statement once.

问题是if语句中的代码在无限循环中调用了可观察对象的下一个函数.这样,它将运行有限次.

The issue is the code inside the if statement calls the observable's next function in an infinite loop. This way it will be run a finite number of times.