2017-06-09 97 views
3

我想檢測form.component.ts上輸入的change事件的值。 我不希望調用函數前:使用此(的onChange)= 「功能($ event.target.value)」Angular 2 Reactive Forms - 檢測組件上的輸入更改事件

public form: FormGroup; 

constructor(private formBuilder: FormBuilder){ 

} 

private loadForm(){ 
    this.form = this.formBuilder.group({ 
     tipo: [null, Validators.required], 
     nomeRazao: [null, Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(64)])], 
     apelidoFantasia: [null, Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(64)])], 
     cpfCnpj: [null, Validators.compose([Validators.required, Validators.minLength(11), Validators.maxLength(14)])], 
     rgIe: [null], 
     contato: this.formBuilder.group({ 
      email: [null], 
      telefone: [null] 
     }), 
     endereco: this.formBuilder.group({ 
      cep: [null, Validators.pattern('^([0-9]){5}([-])([0-9]){4}$')], 
      uf: [null], 
      cidade: [null], 
      bairro: [null, Validators.required], 
      logradouro: [null], 
      complemento: [null], 
      numero: [null, Validators.pattern('/^\d+$/')] 
     }) 
    }); 
} 

ngOnInit() { 
    this.loadForm(); 
} 

回答

7

您可以訂閱你的形式變化:

this.form.valueChanges.subscribe(() => { 
    if (this.registerForm.controls['yourControlName'].value === 'someValue') { 
     // 
    } 
}); 
+1

很好的答案,但更好的類型安全將是: this.form.valueChanges.subscribe((數據)=> { 如果(data.yourControlName === 'someValue中'){// } } ) ; –