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

相关内容

热门资讯

透视学习!pokemmo脚本(... 透视学习!pokemmo脚本(HHpoker正品)本来存在有辅助插件(哔哩哔哩)pokemmo脚本是...
透视资料!如何下载德普之星辅助... 透视资料!如何下载德普之星辅助软件(AApoker透视软件)竟然是真的有辅助教程(哔哩哔哩)1、玩家...
透视妙招!wepoker可以开... 透视妙招!wepoker可以开透视吗(WePoKer规律)真是是有辅助插件(哔哩哔哩)1、透视妙招!...
透视练习!wepoker黑侠辅... 透视练习!wepoker黑侠辅助器(WePoKer开辅助)好像真的是有辅助教程(哔哩哔哩)1、透视练...
透视法门!pokemmo修改器... 透视法门!pokemmo修改器手机版(德普之星官网)原来真的是有辅助神器(哔哩哔哩)1、首先打开po...
透视指南!hhpoker透视脚... 透视指南!hhpoker透视脚本视频(HHpoker必备)总是有辅助软件(哔哩哔哩)在进入hhpok...
透视操作!wepoker辅助器... 透视操作!wepoker辅助器软件下载(AApoker辅助)确实真的有辅助神器(哔哩哔哩)1、wep...
透视大纲!有哪些免费的wpk作... 透视大纲!有哪些免费的wpk作必弊码(HHpoker必备)确实是有辅助神器(哔哩哔哩)1、用户打开应...
透视窍要!epoker透视底牌... 透视窍要!epoker透视底牌(HHpoker安装包)其实真的是有辅助插件(哔哩哔哩)1、epoke...
透视妙计!wepoker透视脚... 透视妙计!wepoker透视脚本是什么(HHpoker德州)本来真的是有辅助app(哔哩哔哩)1、点...