form源代码:
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'jerryform',
template: `
Favorite Color: <input type="text" [formControl]="favoriteColorControl">
`
})
export class ReactFormComponent implements OnInit {
ngOnInit(): void {
this.favoriteColorControl.valueChanges.subscribe(
(value) =>{
console.log('new value: ' + value);
}
)
}
favoriteColorControl = new FormControl('');
}
每当form内input field值发生变化时,通过this.formcontrol.valueChanges发送事件,valueChanges类型为EventEmitter,本质是一个Observable,在下图_initObservables方法里初始化:
一旦input字段的值发生变化后,自动触发updateControl:
FormControl的类型:
FormControlDirective的类型:
更多Jerry的原创文章,尽在:“汪子熙”: