One of the challenges I am facing is handling public fields in my service:
@Injectable()
export class ShareDataService {
// Some code
public templateForCurrencyCOS;
public templateForPercentCOS;
public templateForCurrencyCOGS;
public templateForPercentCOGS;
// Some code
}
The values for these fields are actually set in the HeaderComponent
by subscribing to the revenueService
:
export class HeaderComponent implements OnInit, OnDestroy {
constructor(
private revenueService: RevenueService
) {}
ngOnInit() {
this.addCOSItemCurrencyTemplate();
this.addCOSItemPercentTemplate();
this.addCOGSItemCurrencyTemplate();
this.addCOGSItemPercentTemplate();
}
// Methods to add templates for different items
...
Sometimes, I encounter issues where the values for
this.shareDataService.templateForCurrencyCOGS
, as well as other related fields, become undefined
:
export class RevenueAssistantComponent implements OnInit {
ngOnInit() {
// Assigning values to templates based on shareDataService fields
...
}
}
I suspect that the problem lies in the fact that the RevenueAssistantComponent
initializes before the HeaderComponent
sets values for the fields. To resolve this issue, the RevenueAssistantComponent
should always get the template values after the HeaderComponent
has set the values for the shareDataService
fields. However, I am unsure of how to implement this.