0

AppInitializer class

dataloadcomplete = false;

initializeApp() {
let promise = new Promise((resolve, reject) => {
Promise.all(promise).then(
  (res) => { dataloadcomplete = true; resolve() },
  (err) => { reject(); },
);
return promise;
}

CanactivateRoute

canActivate(route: ActivatedRouteSnapshot): Promise<Boolean> {
 // if data load completed return true else false;

}

want to reinitialize the application on country change.

user3050267
  • 71
  • 1
  • 9
  • Learn about `Subject` and its usage http://jasonwatmore.com/post/2018/06/25/angular-6-communicating-between-components-with-observable-subject – Amit Chigadani Feb 10 '19 at 08:29
  • @ Amit Chigadani yup but value is changed multiple times – user3050267 Feb 10 '19 at 08:56
  • is there any way to retry on promise – user3050267 Feb 10 '19 at 08:57
  • Yes, that is what Subject does for you. You can emit as many times as you want from one component. And listener component will subscribe to it and receive Everytime when new value is emitted. Its an asynchronous programming. – Amit Chigadani Feb 10 '19 at 09:02
  • But in my case , i want to wait until data load is completed. If completed , i will load the application else not. This data load can be started again if there is user change or country change , without url refresh – user3050267 Feb 10 '19 at 09:09
  • Add your actual code in a stackblitz. That should help more. – Amit Chigadani Feb 10 '19 at 09:12
  • Instead of using the `canActivate` guard, use a route resolver. A route resolver will automatically *wait* for the data to be resolved before continuing on to the route. I have an example of a route resolver here: https://stackoverflow.com/questions/43898934/how-to-handle-error-in-a-resolver/43899217#43899217 – DeborahK Feb 10 '19 at 19:20

2 Answers2

0

With the small amount of shown code, it's difficult to suggest the best course of action as there are several options.

First, in any case, be sure that the class that holds the data (Class 1 in your example) is an injectable service. Inject it into any component that needs to access the data.

If you need to load some data and wait to route to a specific component until that data is loaded, you can use route resolvers.

If you need to wait for any of the application to load before launching the application, you can use APP_INITIALIZER: Angular: How to correctly implement APP_INITIALIZER

If you want to better manage your application state, consider using the Redux library for Angular called NgRx. You can use it to manage the flow of data in your application.

DeborahK
  • 57,520
  • 12
  • 104
  • 129
0

If you have multiple HTTP calls, I think, you can use forkJoin to retrieve all the call into one point then use EventEmitter and catch the event from the function of class 2. no need to wait for setting the variable of class 1.

You can use also interval in Angular and wait until you get the true value of the variable of class 2.

Shohel
  • 3,886
  • 4
  • 38
  • 75