简体   繁体   English

Angular 响应式表单 - 按钮禁用方法调用两次

[英]Angular Reactive Form - Button disabled method called twice

I have simple case that show my question.我有一个简单的案例来说明我的问题。

` `

AppComponent.ts

` `

` `

 formGroup = new FormGroup({
    firstName:new FormControl(''),
    lastName: new FormControl('')
  });

  isValid(){
    console.log(this.formGroup);
    return true;
    
  }

` `

` `

AppComponent.html

` `

` `

<form [formGroup]="formGroup">
    <label>FirstName</label>
    <input [formControlName]="'firstName'" />
    <label>Last Name</label>
    <input [formControlName]="'lastName'" />
    <button type="submit" [disabled]="!isValid()" >Submit</button>
</form>

` `

My question is, why I see in the console twice output from isValid method?.我的问题是,为什么我从 isValid 方法在控制台中看到两次 output?。 Is there any reasone for that?有什么理由吗? and will it impact application performance if I use this way with complex form?如果我以这种方式使用复杂的形式,它会影响应用程序性能吗?

note: I woundring why isValid method called twice?注意:我 woundring 为什么 isValid 方法被调用了两次?

I am excepting the method to be called once.我排除了被调用一次的方法。

Let me explain to you.让我给你解释一下。 There is a thing in angular called Change Detection . angular 里面有个东西叫Change Detection This is an internal mechanism inside of Angular .这是Angular内部的一个内部机制。 This change detection mechanism identified the changes in the UI , input, output properties, and value changes in the properties that are binded to different elements on the UI.此更改检测机制可识别UI中的更改、输入、output 属性以及绑定到 UI 上不同元素的属性中的值更改。

So whenever any change is detected in the whole application, then angular runs a change detection cycle to update all the properties values along with UI.因此,只要在整个应用程序中检测到任何更改,angular 就会运行一个change detection周期来更新所有属性值以及 UI。

Now, let's come to your question.现在,让我们来回答你的问题。 You are calling a function in the template which is isValid .您在isValid模板中调用 function 。 So what's happening here is that, whenever any change is detected in the UI then angular is running change detection twice while rendering the whole UI that's why you are seeing the console.log twice.所以这里发生的事情是,每当在UI中检测到任何更改时,angular 都会在呈现整个 UI 时运行两次更改检测,这就是为什么您会看到两次console.log的原因。

Why is calling change detection twice?为什么调用变化检测两次?

The answer is, you bind your formGroup in the UI to the form element.答案是,您将UI中的formGroup绑定到form元素。 So initially, your formGroup is undefined, but later on in your code you are creating a reactive form .所以最初,您的formGroup是未定义的,但稍后在您的代码中您正在创建一个reactive form So when reactive form gets created then, then the value of formGroup changes from undefined to an actual reactive form object. So in this way, the value of formGroup changes 2 times.因此,当创建reactive form时, formGroup的值从未undefined更改为实际的reactive form object。因此,以这种方式, formGroup的值更改了 2 次。

Will it impact the application's performance?它会影响应用程序的性能吗?

Yes, it will impact the application's peformance drastically, that's why it is recommended in angular, not to call functions directly in html template.是的,它会极大地影响应用程序的性能,这就是为什么在 angular 中建议,不要直接在html模板中调用函数。 Use properties instead.请改用属性。 Please read this as a reference to get more understanding请阅读此作为参考以获得更多理解

Why you shouldn't use functions in the template 为什么不应该在模板中使用函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM