简体   繁体   中英

Angular Wait until function has been executed

I am currently programming a page in Angular. As soon as a page is called, the OnInit method is used to execute a function that initializes multiple variables. When this is done, an if query should be used to check whether a variable has a certain value (which is initialized in the function before).

How can I make sure that the if query is not made until the function is finished?

And is it possible to execute a function when starting the app?

It might be better for you to initialize some of your variables in the constructor for the component and then do your if check in the onInit function. The lifecycle hook for Angular has the constructor code executed before the onInit function.

Add the query stuff within the function? or Convert the function into maybe a subscription or promise and then do something once that resolves?

Observable.subscribe(value=> { // do something here });

Promise.then(value => // do something here);

or even maybe call that function or that code within the function you need to wait to complete.

You can execute a function when starting the application bootstrap.

You have to revert to promise for that and follow this -- may have changed for v6, but should be similar concept.

Angular 2: Call service before bootstrap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM