I'm using Rxjs, and I want to create an Observable from the following pattern (in order):
get params from paramMap$, then ...
based on the value of params, get both (getData():Observable, getCategories():Observables) together, then ....
from ([data, categories]) create the final object.
The code would look like:
//route = this.ActivatedRoute
let obs$ = route.paramMap.pipe(
// combineLatest() is not a pipable operator, so it shouldn't be used inside .pipe()
// also it doesn't accept a function,
// but this just to show you what I want to do.
combineLatest(params=>getData(params.id), params=>getCategories(params.id)),
map(([data, categories])=>{
//do some changes...
return {data,categories}
}
)
)
also where is the best place to put this code in Angular project:
constructor()
is not the best practice, because this is a long-running operation (actually it has to do API requests)ngOnInit()
is not recommended, because in some point I have to changethis.params
which is used in the template
...
combineLatest(params=>
getData(params.id).pipe(
map(data=>{
this.params.type=data.type;
return data
})),
params=>getCategories(...)
)
in template:
<div>{{params.type}}</div>