I am bit struggling with the observable. I have some data in my api which i am returning as observable (using behavioralsubject and exposing as a get property) input (async) to my child component. If I am binding the data to the UI directly using safe operator then it is fine. Because whenever it gets subscribed UI will be updated. But my problem is that i want to do some operations like filter and map to that input. I need this operation to be done as component loads, if i try to do so i get undefined, because observable is not subscribed yet. I need a solution for this which is efficient. I don't want to subscribe it again in to my class component as I am already subscribing it in the html using async.
Asked
Active
Viewed 333 times
0
-
Do what you need to do, inside the callback: `.subscribe(data => {this.data = data; // do stuff here with data})` – AT82 Jun 12 '17 at 20:14
-
I mentioned, if i don't want to subscribe it in ngOnInit(). Is there any way to do that – Deepender Sharma Jun 13 '17 at 04:52
-
Ah see, I missed the last line, sorry about that. If you want to do filtering and mapping you'd want to subscribe in component to do that. Or then you can perhaps flip it around and do something like the latter part of this answer? https://stackoverflow.com/a/37867462/6294072 ? – AT82 Jun 13 '17 at 05:08
-
Ok, I have to subscribe for that. Anyways I have figured out the way to do this. What i am doing is, I am initializing the default value for my Input and then passing to child. This way it is not undefined and initial values are visible. – Deepender Sharma Jun 13 '17 at 12:55
1 Answers
0
Try using resolver
which will pre-fetch data before loading the component.
https://angular.io/docs/ts/latest/guide/router.html#!#resolve-guard

Nehal
- 13,130
- 4
- 43
- 59