Angular的FormControl导致无限循环。
创始人
2024-10-24 03:31:25
0

可能会出现这个问题的原因是在监听表单控件的值变化时,代码同时更改了表单控件的值,导致了无限循环。为了避免这个问题,可以将更改控制器值的代码放在Angular的zone之外,使其避开Angular的变更检测。

示例代码:

import { Component, NgZone } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'my-app',
  template: `
    

Form value: {{ formGroup.value | json }}

`, }) export class AppComponent { formGroup = new FormGroup({ name: new FormControl('') }); constructor(private ngZone: NgZone) { this.formGroup.get('name').valueChanges.subscribe(name => { // This code will cause an infinite loop // this.formGroup.setValue({ name: name.toUpperCase() }); // Putting the code outside of Angular zone // will avoid the infinite loop this.ngZone.runOutsideAngular(() => { this.formGroup.setValue({ name: name.toUpperCase() }); }); }); } }

相关内容

热门资讯

记者获悉!wpk辅助购买(透视... 记者获悉!wpk辅助购买(透视)一直是有挂(有挂解惑开挂辅助安装)-哔哩哔哩一、wpk辅助购买软件透...
目前!aapoker俱乐部靠谱... 目前!aapoker俱乐部靠谱吗,wepoker轻量版有透视吗,机巧教程(有挂解密)-哔哩哔哩一、w...
此事引发网友热议!来玩德州破解... 此事引发网友热议!来玩德州破解器(透视)原来真的有挂(确实有挂开挂辅助工具)-哔哩哔哩1、此事引发网...
据权威媒体报道!cloudpo... 据权威媒体报道!cloudpoker辅助,扑克之星辅助,学习教程(有挂详情)-哔哩哔哩1)扑克之星辅...
迎来新发展!pokemmo脚本... 迎来新发展!pokemmo脚本辅助器(透视)一贯是有挂(有挂教学开挂辅助插件)-哔哩哔哩1)poke...
此事引发网友热议!aapoke... 此事引发网友热议!aapoker透视脚本,wepoker辅助脚本,烘培教程(有挂秘籍)-哔哩哔哩1、...
为切实保障!wepoker好友... 为切实保障!wepoker好友房开挂(透视)原来有挂(有挂透明挂开挂辅助安装)-哔哩哔哩1、实时we...
无独有偶!hhpoker是真的... 无独有偶!hhpoker是真的假的,wpk辅助器,法子教程(有挂攻略)-哔哩哔哩一、hhpoker是...
出现新变化!wepoker究竟... 出现新变化!wepoker究竟有没有透视(透视)果然有挂(真实有挂开挂辅助辅助器)-哔哩哔哩1、we...
据报道!aapoker公共底牌... 据报道!aapoker公共底牌,wepoker公共底牌,模板教程(的确有挂)-哔哩哔哩暗藏猫腻,小编...