I have encountered an issue where the data retrieved from a server in a service is available in the component's template but not in the actual code. This seems strange to me.
I made the call in the ngOnInit
method of my main AppComponent
ngOnInit() {
this.dataContractService.getUIStrings();
}
This method is called before any other component's ngOnInit
, so the strings should be available to them.
Here is the code for getUIStrings
:
async getUIStrings() {
let url = "/datacontract/getuistrings";
let promise = await this.http.get<APIResponse>(url).pipe(map(x => { return x.responsePayload })).toPromise<any>();
this.globals.UIStrings = promise.strings; //
}
The results are stored in globals
, which is an injectable service used by components that need access to UIStrings
.
Here is an example of how I am using it:
export class DashboardComponent {
constructor(
public globals: Globals,
) { }
dashboardMessage: string;
ngOnInit() {
if (condition) {
this.dashboardMessage = this.globals.UIStrings["DashboardAdminDesc"];
}
else {
this.dashboardMessage = this.globals.UIStrings["DashboardDesc"];
}
}
}
In the code, UIStrings
is an empty array, but in the component's template above, it is available and populated.
{{globals.UIStrings["DashboardAdminDesc"]}}
This setup seems to work. Can anyone point out what I might be doing wrong here?