5

I have an angular2 RC6 application and I need to defer loading pages until after some functionality is executed.

I have a service that determines the URI for the webApi. I need the application to wait until this URL is determined before it tries to load any data. If I hide the main application in app.component, with an *ngIf, I get routing errors because the router outlet does not exists.

Before RC5/6 I manually bootstrapped the application after the service get's the URI.

Don Chambers
  • 3,798
  • 9
  • 33
  • 74
  • 1
    Hard to say without seeing code but you can try to use Observable or Promise in your service and then wait until it completes. – rook Sep 12 '16 at 21:26
  • But I can't bootstrap when it completes. In RC6 you add the bootstrap to NgModule and I don't know how to make it conditional. – Don Chambers Sep 12 '16 at 21:35
  • Why do you want to defer the loading of the entire page (templates)? Aren't you just waiting for data from an API? – C14L Sep 12 '16 at 21:35
  • Yes. But there are API calls throughout the application. With deep URL linking the user could be going to any page and using any API. I would have to put a check on all of them. We have already built it using the process of bootstraping when it's ready and I just need to move it to RC6. Plus, I don't want another manual task we have to do to do any call. It seems cleaner if we just wait until it's ready to bootstrap it - or to load any page. – Don Chambers Sep 12 '16 at 21:43
  • http://stackoverflow.com/questions/37611549/how-to-pass-parameters-rendered-from-backend-to-angular2-bootstrap-method/37611614#37611614, http://stackoverflow.com/questions/36517961/resolve-a-value-before-activating-a-route-in-angular-2/36518012#36518012 (or http://stackoverflow.com/questions/38425461/angular2-canactivate-calling-async-function/38425489#38425489) – Günter Zöchbauer Sep 13 '16 at 05:35

1 Answers1

3

If you use the router you can use canActivate, canActivateChild.

See also

For loading once before app initialization you can use APP_INITIALIZER as explained in How to pass parameters rendered from backend to angular2 bootstrap method

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567