How To Assign Data From Service To Json?
I am making angular 6 application, where i am making a angular dynamic form in which the data comes as dynamic from JSON. JSON: jsonData: any = [ { 'elementType': 'text
Solution 1:
you must ask yourself about what data you receive and what data do you need. In general you can has,e.g.
getOptions():Observable<any[]>
{
//of, create an observable
return of(
[{key:"option1",data:["option1_1","option_1_2"]},
{key:"option2",data:["option2_1","option_2_2","option_2_3]},..
])
}
getModel():Observable<any[]>
{
return of(
[{key:"option1",...},
{key:"option2",..}
])
}
You must use switchMap to received the fullModel. switchmap make that you don't receive the first call else the inner one. is a way to not encatenate subscribe
getFullMode():Observable<any[]>
{
returngetOptions().pipe(switchMap(
opts=>{
returngetModel().pipe(map(mod=>{
//..here transform "mod" using the values of opts//e.g.let option=opts.find(p=>p.key=mod.key);
mod.options=option.data
}))
})
)
}
Well, you case is easer because only has an "option" and a unique "option", anf json is fixed.
getFullJson(url,token)
{
this.service.get(url,token).pipe(map(opt=>{
//we don't want return opt, else this.jsonData transformed.let element=this.jsonData.find(e=>e.elementType=='dropdown');
if (element)
element.option=res.datareturnthis.jsonData
}))
}).subscribe(res=>console.log(res));
If your json comes from an observable, not use map, use switchmap. SwitchMap, wait the outer call end to make the second
getFullJson(url,token) {
this.service.get(url,token).pipe(switchMap(opt=>{
//we don't want return opt, else this.jsonData transformed.//to transform data use pipe(map)returnthis.service.getJsonData(pipe(map(jsonData=>{
let element=jsonData.find(e=>e.elementType=='dropdown');
if (element)
element.option=res.datareturnthis.jsonData
}))
}).subscribe(res=>console.log(res));
Post a Comment for "How To Assign Data From Service To Json?"