I have a child component that looks like this:
@Component({
selector: 'app-child',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
{{text}}
`
})
export class ChildComponent {
@Input() text = '';
constructor(public host: ElementRef) { }
}
And a parent component that looks like this:
@Component({
selector: 'app-parent',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<ng-content></ng-content>`
})
export class ParentComponent {
@ContentChild(ChildComponent) child: ChildComponent;
constructor(private cdr: ChangeDetectorRef) { }
ngAfterContentInit() {
this.child.text = 'hello';
this.child.host.nativeElement.addEventListener('click', () => {
this.child.text = 'from click';
this.cdr.detectChanges();
});
}
The first assign to the text
property is working fine, but when I click the button and try to change the text
property again nothing is happening.
That's confusing because from what I know:
1. The click event should trigger change detection and the text property is different so it should have been updated.
2. I explicitly called detectChanges()
and this should check also the children from what I know.
What am I missing?