I am restructuring an angular project where I found below functionality.
In app.component.ts file
ngOnInit() {
this.portfolioID = Number(sessionStorage.getItem('portfolioID'));
console.log(this.portfolioID);
this.sharedService.getPortfilioID().subscribe(portfolioID => {
if (portfolioID && portfolioID !== 0) {
this.items.forEach(item => {
if (item.displayText === 'Business Licenses' && item.items.length > 0) {
item.items.forEach(childItem => {
childItem.url = childItem.defaultUrl;
});
}
});
}
});
}
They are getting some Id from localstorage which was set by clicking on a link in another component.
Another.component.html
<ng-template kendoGridCellTemplate let-dataItem *ngIf="column.isActionable && titleName == 'Customer Search'">
<a href="javascript:void(0)" *ngIf="column.attributeName == 'portfolioName'"
(click)="viewPortfolioDetails(dataItem.portfolioID)">{{dataItem.portfolioName}}</a>
</ng-template>
Another.component.ts
viewPortfolioDetails(portfolioID: number) {
sessionStorage.setItem('portfolioID', portfolioID.toString());
this.sharedService.setPortfolioID(portfolioID);
}
So, as it was set on click, initially when page loads first time before click, I am not able to get the ID and the subsequent functionality is not working. So, my question is can I call the onInit() method of the app.component again? Or is there any better approach to do this. Thanks.