简体   繁体   English

Angular2 ControlGroup valueChanges初始绑定

[英]Angular2 ControlGroup valueChanges on initial bind

I have a form with some <input type="text"> widgets and I have noticed that ControlGroup.valueChanges is being called upon initial databind when using [ngFormModel] and ngControl . 我有一个带有一些<input type="text">小部件的表单,我注意到在使用[ngFormModel]ngControl时,在初始数据绑定时调用了ControlGroup.valueChanges

This means that the user thinks that the form has been changed upon initial load. 这意味着用户认为表单在初始加载时已更改。

Is this normal or should I be using a different observable to track changes made the by the user? 这是正常的还是我应该使用不同的observable来跟踪用户所做的更改?

I am using Angular2 RC3 and the following version import for forms: 我正在使用Angular2 RC3和以下版本导入表单:

import {ControlGroup, Validators, FormBuilder} from '@angular/common';

I think that's just how it works, however if you just want to track if changes are made by user, you should employ ControlGroup.dirty or formControl.dirty with the changes Observable. 我认为这就是它的工作原理,但是如果你只想跟踪用户是否进行了更改,则应该使用带有Observable changes ControlGroup.dirtyformControl.dirty

ControlGroup.valueChanges.subscribe(() => {

 if(ControlGroup.dirty){
   console.log('This change is made by User.');
 } 
 else {
   console.log('This change is Automated. before any User interaction.');
 }

})

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

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