I have an angular2 (with angular-cli) app that will exist inside a web page of a website that is provided by a CMS (DotnetNuke). This web cms uses the services framework of jquery to generate a validation token for api calls to the website's "backend". As a result I can not generate any web api calls to the website until I have this validation token.
I created a service to handle the calls to the website's api(s). In the constructor of the service I try to set the token in the following way (the ServicesFramework Object has a function called getAntiForgeryValue which returns the token ):
@Injectable()
export class ApiService {
private http: Http;
constructor(http: Http)
{
console.log('dnn service constructor');
$(document).ready(function () {
this._dnnSF = $.ServicesFramework(804);
});
}
getRequestVerificationToken(): any {
return this._dnnSF.getAntiForgeryValue();
};
}
The getAntiForgeryValue() of the ServicesFramework just returns the jQuery __RequestVerificationToken for ajax calls to .net web api endpoints. This is just for explanation.
The Issue:
The other components that get this service injected are calling the getAntiForgeryValue() before the document.ready is complete in the services's constructor and therefore I am getting:
Cannot read property 'getAntiForgeryValue' of undefined
Question: Can someone help me work out a strategy with angular2 where I can wait for this validation token to be provided before the angular app runs or how to wait in the service for the constructor to complete.
Thanks in advance