Angular Form Control valueChanges not working

M.J

I want to get one of my forms ("family") value if changed by subscribe but it seems something wrong, because I got nothing on my console's log.

import { Component , AfterViewInit } from '@angular/core';
import {FormGroup,FormBuilder} from '@angular/forms';
import {Observable} from 'rxjs/Rx';

@Component({
selector: 'app-root',
template: `<h1>Hello World!</h1>
            <form [formGroup]="frm1">
            <input type="text" formControlName="name" >
            <input type="text" formControlName="family">
            </form>
            `,

})

export class AppComponent implements AfterViewInit{ 

 frm1 : FormGroup;

constructor( fb:FormBuilder){
    this.frm1 = fb.group({
        name : [],
        family: []
    });     
}
ngAfterViewInit(){  
    var search = this.frm1.controls.family;
    search.valueChanges.subscribe( x => console.log(x));    
}
}
Amit Chigadani

Use get method on form variable frm1. And use ngOnInit() instead of ngAfterViewInit()

ngOnInit() {  
   this.frm1.get('family').valueChanges.subscribe( x => console.log(x));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Form control valueChanges gives the previous value

enable/disable form control fires valueChanges Angular 2 Forms

Angular form not working in a table

Angular 2 form valueChanges fires continuously

Skip programmatic changes in valueChanges of Angular 2 control

Should I use `registerOnChange` or `valueChanges` on a form control

Angular2 FormGroup - How to determine which control triggered valuechanges

Angular 6 - rxjs pipe not working on valueChanges

Angular 6 Form - Mutate Value after ValueChanges

Angular 2+: how to subscribe to form valueChanges of whitelisted fields?

Is it necessary to unsubscribe Angular Form statusChanges/valueChanges observable?

Angular Reactive Forms form control valueChanges stops listening on error

Angular Reactive Form - How to add subscribe valueChanges to a single control inside Form Control in Form Array

Angular Reactuve Form Control valueChanges get value changed field name , prev value and current value

Toggle between a formarray controls and another form control is not working in angular?

Angular Subscribe valuechanges in formarray in form group

Angular Reactive Form, valueChanges runs into "maximum call stack size"

Using an Angular component as form control

How to watch the valueChanges of a form and get the form control name?

Angular taking control of the Form

form validation not working in angular

Angular submission form not working

Angular reactive form valueChanges function to detect and store only changed fields

Form validation is not working in angular?

Angular 5 reactive form valueChanges does not work

Angular 2 Form Control Update

Angular - Reactive form valueChanges. Avoid nested subscriptions

Combine observables from valuechanges and statuschanges reactive form with rxjs in angular

How to get a form control valueChanges value before subscription?