How can I trigger ngOnChanges only after every input values has been set in ngOnInit. My code -->
`ngOnInit() {
this.multiSelectorData.forEach(item=>{
this.allItems.push(item.data);
});
}
ngOnChanges(changes: SimpleChanges) {
if(changes['dataReady'] && changes['dataReady'].currentValue) {
console.log(this.allItems);
}`
I wont get the array I initialized in ngonit in ngonchanges because ngonchanges is called before ngonit, how can i check if all are initialized before checking ngonchanges?
One method I have found that works is based on the fact that the input values all have a previous value property. If the input has not previously been set then that value will be
CD_INIT_VALUE
(as a string). So you can make a condition that your block of code inngOnChanges
should only run if the previous value is notCD_INIT_VALUE
. Here's an example for your case where you're testing ALL the input values:There are probably more elegant solutions but I've found this works. This is probably too late to help you but may help others as when I googled this I found this question. The solution was something I figured out from debugging.