【发布时间】:2017-07-28 19:54:12
【问题描述】:
我有一个用FormBuilder 构建的简单表单:
this.contactForm = formBuilder.group({
'name': [''],
'email': [''],
'phone': [''],
});
我想观察每个控件的变化,并在发生这种情况时运行具有更新值的函数:
getContacts(value: any) {
this.phoneContacts.findContact(value).then(
contacts => {
// do something
}
);
}
现在,我正在为每个控件执行此操作,使用 bind 访问组件的 this 对象:
this.contactForm.get('name').valueChanges.subscribe(this.getContacts.bind(this));
this.contactForm.get('email').valueChanges.subscribe(this.getContacts.bind(this));
this.contactForm.get('phone').valueChanges.subscribe(this.getContacts.bind(this));
有没有什么方法可以监视更改并仅通过一次订阅获得更新的值?我知道我可以直接订阅this.contact.form,但我得到的不仅仅是更新的控件,而是所有控件的值。
【问题讨论】:
-
所以如果我理解正确的话,你想要像
this.contactForm.subscribeToUpdated()这样的东西? -
没错!
subscribetoUpdated()会给我上次更改输入的值