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() }); }); }); } }

相关内容

热门资讯

受玩家影响!wepoker透视... 受玩家影响!wepoker透视脚本苹果版(透视)切实存在有辅助app(有挂教程)-哔哩哔哩1、wep...
据公告内容!sohoo竞技联盟... 据公告内容!sohoo竞技联盟辅助器(透视)好像是真的辅助神器(有挂分享)-哔哩哔哩sohoo竞技联...
透视模拟器!hhpoker透视... 透视模拟器!hhpoker透视方法(透视)一贯真的是有辅助工具(有挂规律)-哔哩哔哩1、hhpoke...
透视模拟器!大菠萝免费辅助器(... 透视模拟器!大菠萝免费辅助器(透视)都是真的有辅助神器(有挂猫腻)-哔哩哔哩1、每一步都需要思考,不...
此事引发网友热议!大菠萝免费辅... 此事引发网友热议!大菠萝免费辅助器(透视)真是是有辅助工具(有挂方法)-哔哩哔哩1、打开软件启动之后...
透视好友!wepoker辅助是... 透视好友!wepoker辅助是真的吗(透视)都是存在有辅助神器(有挂秘籍)-哔哩哔哩1、任何wepo...
连日来!wepoker免费透视... 连日来!wepoker免费透视(透视)好像是真的辅助插件(有挂秘籍)-哔哩哔哩1、点击下载安装,we...
据了解!wepoker透视脚本... 据了解!wepoker透视脚本免费使用视频(透视)一贯真的是有辅助软件(有挂分析)-哔哩哔哩;1、让...
透视实锤!hhpoker德州透... 透视实锤!hhpoker德州透视挂(透视)都是真的有辅助神器(有挂教程)-哔哩哔哩1、hhpoker...
刚刚!wpk有辅助器吗(透视)... 刚刚!wpk有辅助器吗(透视)好像有辅助工具(有挂功能)-哔哩哔哩;1、进入到wpk有辅助器吗是否有...