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方法里初始化:
FormControl的类型: