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有没有机器人,hhpoker脚本下载,诀窍教程(原来是有挂)-哔哩哔哩一、...
透视苹果版本!德州透视是真的吗... 透视苹果版本!德州透视是真的吗(透视)开挂辅助器(真是是有挂)-哔哩哔哩在进入德州透视是真的吗辅助挂...
透视挂透视!steampoke... 透视挂透视!steampokermaster辅助,wepoker辅助软件价格,教程书教程(本来存在有...
透视机器人!wepoker开脚... 透视机器人!wepoker开脚本视频(透视)开挂辅助app(一直是真的挂)-哔哩哔哩1、许多玩家不知...
透视讲解!德州局脚本,wepo... 透视讲解!德州局脚本,wepoker插件程序激活码,教程书教程(一直存在有挂)-哔哩哔哩1、wepo...
透视苹果版本!智星德州可以透视... 透视苹果版本!智星德州可以透视吗(透视)开挂辅助脚本(真是是真的挂)-哔哩哔哩1、完成智星德州可以透...
透视脚本!德州之星扫描器,we... 透视脚本!德州之星扫描器,wejoker辅助脚本,学习教程(切实有挂)-哔哩哔哩亲,关键说明,德州之...
透视大厅!wepoker透视脚... 透视大厅!wepoker透视脚本是什么(透视)开挂辅助脚本(果然真的是有挂)-哔哩哔哩1、透视大厅!...
透视神器!hhpoker德州透... 透视神器!hhpoker德州透视,We poker辅助器下载,法子教程(一贯真的是有挂)-哔哩哔哩1...
透视线上!wepoker游戏的... 透视线上!wepoker游戏的安装教程(透视)开挂辅助技巧(真是有挂)-哔哩哔哩1、很好的工具软件,...