ngModelChange输入上的无限循环

问题描述 投票:0回答:1

我有这样的html:

<ng-template [ngSwitchCase]="'textbox'">
    <input *ngIf="setting.type==='number'"
           [step]="setting.step"
           [formControlName]="formName"
           [id]="formName"
           [type]="setting.type"
           [placeholder]="setting.placeholder"
           [title]="setting.description"
           (ngModelChange)="onChange($event)">
</ng-template>

并且在控制器上,我具有onChange函数:

onChange(newValue: string) {
    if (newValue === undefined)
        return;

    this.form.get(this.formName).setValue(<any>parseFloat(newValue));
}

[当我调试此onChange函数的调用时,我注意到它仍在调用,真的不知道为什么。我有一个无限循环。

我的角度包:

"@angular/animations": "8.2.7",
"@angular/cli": "8.3.5",
"@angular/common": "8.2.7",
"@angular/compiler": "8.2.7",
"@angular/core": "8.2.7",
"@angular/forms": "8.2.7",
"@angular/platform-browser": "8.2.7",
"@angular/platform-browser-dynamic": "8.2.7",
"@angular/router": "8.2.7",
"@babel/polyfill": "7.6.0",

您是否知道我的代码可能有什么问题?

angular typescript angular7 infinite-loop angular-ngmodelchange
1个回答
0
投票

可能是由于在onChange函数中设置了输入值,该函数再次更改了输入值并再次调用onChange。这无限地继续。

© www.soinside.com 2019 - 2024. All rights reserved.