This made your plunker working:
- add this import statement in app.ts
import 'rxjs/add/operator/do';
- Remove semicolon after .do statement
.do(changes => {
console.log('name has changed:', changes)
})
Complete changed app.ts
import {Component, NgModule, OnInit} from '@angular/core'
import {BrowserModule } from '@angular/platform-browser'
import { FormGroup, FormBuilder, Validators, ReactiveFormsModule, FormsModule } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/operator/do';
@Component({
selector: 'my-app',
template: `
<form novalidate [formGroup]="form">
<div>
<select type="text" class="form-control" name="title" formControlName="title">
<option *ngFor="let title of titles" value="{{title}}">{{title}}</option>
</select>
</div>
<div>
<input type="text" class="form-control" name="name" formControlName="name">
</div>
<fieldset formGroupName="address">
<legend>Address</legend>
<input type="text" class="form-control" name="street" formControlName="street">
<input type="text" class="form-control" name="city" formControlName="city">
</fieldset>
</form>
`,
})
export class App implements OnInit {
private form: FormGroup;
private titles: string[] = ['Mr', 'Mrs', 'Ms'];
constructor(private fb: FormBuilder){
}
ngOnInit() {
this.form = this.fb.group({
title: 'Mr',
name: '',
address: this.fb.group({
street: '',
city: ''
})
});
this.form.get('name').valueChanges
.do(changes => {
console.log('name has changed:', changes)
})
.subscribe();
this.form.get('title').valueChanges.subscribe(
changes => console.log('title has changed:', changes)
);
this.form.get('address').valueChanges.subscribe(
changes => console.log('address has changed:', changes)
);
}
}
@NgModule({
imports: [ BrowserModule, FormsModule, ReactiveFormsModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
Another solution approach would be to use the "ngModelChange" event
Change this in your template:
<input type="text" class="form-control" (ngModelChange)="doNameChange($event)" name="name" formControlName="name">
In your component you handle then your change event:
doNameChange(event: any){
alert("change")
}