RangeError:使用valueChanges.subscribe

时间:2017-12-14 20:49:11

标签: angular typescript angular5 angular-reactive-forms

我正在使用带有Reactive表单的Angular 5,并且需要使用valueChanges来动态禁用所需的验证

组件类:

export class UserEditor implements OnInit {

    public userForm: FormGroup;
    userName: FormControl;
    firstName: FormControl;
    lastName: FormControl;
    email: FormControl;
    loginTypeId: FormControl;
    password: FormControl;
    confirmPassword: FormControl;
...

ngOnInit() {
    this.createFormControls();
    this.createForm();
    this.userForm.get('loginTypeId').valueChanges.subscribe(

            (loginTypeId: string) => {
                console.log("log this!");
                if (loginTypeId === "1") {
                    console.log("disable validators");
                    Validators.pattern('^[0-9]{5}(?:-[0-9]{4})?$')]);
                    this.userForm.get('password').setValidators([]);
                    this.userForm.get('confirmPassword').setValidators([]);

                } else if (loginTypeId === '2') {
                    console.log("enable validators");
                    this.userForm.get('password').setValidators([Validators.required, Validators.minLength(8)]);
                    this.userForm.get('confirmPassword').setValidators([Validators.required, Validators.minLength(8)]);

                }

                this.userForm.get('loginTypeId').updateValueAndValidity();

            }

        )
}
createFormControls() {
    this.userName = new FormControl('', [
        Validators.required,
        Validators.minLength(4)
    ]);
    this.firstName = new FormControl('', Validators.required);
    this.lastName = new FormControl('', Validators.required);
    this.email = new FormControl('', [
      Validators.required,
      Validators.pattern("[^ @]*@[^ @]*")
    ]);
    this.password = new FormControl('', [
       Validators.required,
       Validators.minLength(8)
    ]);
    this.confirmPassword = new FormControl('', [
        Validators.required,
        Validators.minLength(8)
    ]);

}

createForm() {
 this.userForm = new FormGroup({
      userName: this.userName,
      name: new FormGroup({
        firstName: this.firstName,
        lastName: this.lastName,
      }),
      email: this.email,
      loginTypeId: this.loginTypeId,
      password: this.password,
      confirmPassword: this.confirmPassword
    });
}

然而,当我运行它时,我得到一个浏览器javascript错误

UserEditor.html:82 ERROR RangeError: Maximum call stack size exceeded
    at SafeSubscriber.tryCatcher (tryCatch.js:9)
    at SafeSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscription.js.Subscription.unsubscribe (Subscription.js:68)
    at SafeSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.Subscriber.unsubscribe (Subscriber.js:124)
    at SafeSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:242)
    at SafeSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.SafeSubscriber.next (Subscriber.js:186)
    at Subscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.Subscriber._next (Subscriber.js:127)
    at Subscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.Subscriber.next (Subscriber.js:91)
    at EventEmitter.webpackJsonp.../../../../rxjs/_esm5/Subject.js.Subject.next (Subject.js:56)
    at EventEmitter.webpackJsonp.../../../core/esm5/core.js.EventEmitter.emit (core.js:4319)
    at FormControl.webpackJsonp.../../../forms/esm5/forms.js.AbstractControl.updateValueAndValidity (forms.js:3377)

“记录下来!”被重复记录,就像它被递归调用一样,这就是为什么它们是一个堆栈错误

如果我删除了valueChanges.subscribe代码,那么除了有条件地删除验证之外。

为什么它会递归调用valueChanges.subscribe?

4 个答案:

答案 0 :(得分:10)

尝试在distinctUntilChanged()之前的管道中添加subscribe()。它应该过滤掉那些"改变"事实上价值没有实际改变。

答案 1 :(得分:10)

实际上,真正的答案是,如果你想订阅任何表单更改并且仍然在其中运行patchValue,那么你应该只为patchValue添加{emitEvent:false}选项,因此修补不会触发另一个更改检测

代码:

this.formGroup
    .valueChanges
    .subscribe( _ => {
        this.formGroup.get( 'controlName' ).patchValue( _val, {emitEvent: false} );
    } );

PS。这也比逐个订阅每个表单控件以避免触发更改超出最大调用堆栈更为繁琐。特别是如果你的表单有100个控件来订阅。

现在进一步详细说明,如果您仍然需要在订阅中更新ValueAndValidity,那么我建议您使用distinctUntilChanged rxjs运算符,仅在某些值发生更改时运行订阅。

现在我们还必须使它成为自定义验证函数,因为默认情况下,distinctUntilChanged通过指针验证对象,并且每次更改时指针都是新的。

this.formGroup
    .valueChanges
    .distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b))
    .subscribe( _ => {
        this.formGroup.get( 'controlName' ).patchValue( _val, {emitEvent: false} );
        this.formGroup.get( 'controlName' ).updateValueAndValidity();
    } );

瞧,我们正在修补和更新,而不会遇到最大的调用堆栈!

答案 2 :(得分:8)

问题是您修改了同一字段的valueChanges事件处理程序内部字段的值,导致事件再次被触发:

this.userForm.get('loginTypeId').valueChanges.subscribe(
  (loginTypeId: string) => {
    ...
    this.userForm.get('loginTypeId').updateValueAndValidity(); <-- Triggers valueChanges!
}

答案 3 :(得分:6)

我的回答是this one的发展。

通过在distinctUntilChanged()之前的管道中添加subscribe(),可以避免“超出最大调用堆栈大小”,因为

  

distinctUntilChanged 方法仅在当前值不同于最后一个值时发出。

用法:

this.userForm.get('password')
  .valueChanges.pipe(distinctUntilChanged())         
  .subscribe(val => {})

Documentation