Below is the explanination provided for OnChanges life cycle event in angular2 documentation.
ngOnChanges - before ngOnInit and when a data-bound input property value changes.
Below is my sample component code
@Component({
selector:'app-main',
template:`
<user-form>Loading....</user-form>
`,
directives: [userFormComponent],
providers: [HTTP_PROVIDERS,UserService]
})
export class AppComponent implements OnInit , OnChanges {
@Input()
public prop: string = "hi!";
ngOnInit() {
console.log('ngOnInt') ;
}
ngOnChanges() {
console.log('ngOnChanges') ;
}
}
And I am refering to this component in my html page like this
<app-main [prop]="test">Loading...</app-main>
Now my question is, even though I am changing databound input property "prop" by assiging value "test", why is that ngOnChanges is not executed. By the way I am new to angular2.
AppComponent). It should work the way it was described on inner components. May due to boostrap process. - Harry Ninhtestcome from? Normally that would be a property on the parent component, but since it's your root app component, there's no component for your index html with a 'test' property to pass to[prop]. - Jason Goemaat