I was having the same issue, and this is a simple but not very elegant workaround I am using. Pass in another property to force trigger ngOnChanges method
<div poll-stat-chart [barData]="barData" [changeTrigger]="changeTrigger"></div>
In the parent component class, whenever you want to manually fire the ngOnChanges method on child component, just modify "changeTrigger" property
ParentComponent Class (poll-stat-chart is the child component)
@Component({
directives: [PollStatChartCmp],
template: `
<div poll-stat-chart [barData]="barData" [changeTrigger]="changeTrigger">
</div>
<button (click)="triggerChild()"></button>
`
}
export class ParentComponent {
changeTrigger = 1;
barData = [{key:1, value:'1'}, {key:2, value'2'}];
triggerChild() {
this.barData[0].value = 'changedValue';
//This will force fire ngOnChanges method of PollStatChartComponent
this.changeTrigger ++ ;
}
}
And then in child component class, add a property [changeTrigger]
@Component({
selector: '[poll-stat-chart]',
inputs: ['barData', 'changeTrigger'],
template: `
<h4>This should be a BAR CHAR</h4>
`
})
export class PollStatChartCmp {
barData;
changeTrigger;
constructor(private elementRef: ElementRef) {
this.render();
}
ngOnChanges(changes) {
console.log('ngOnChanges fired');
this.render();
}
render() { console.log('render fired');}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…