如何将服务中的数据分配给 JSON?
How to assign data from service to JSON?
我正在制作 angular 6 应用程序,我正在制作一个 angular 动态表单,其中数据来自 来自 JSON.
JSON:
jsonData: any = [
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_name",
"label": "Project Name",
"type": "text",
"value": "",
"required": false,
"minlength": 3,
"maxlength": 20,
"order": 1
},
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_desc",
"label": "Project Description",
"type": "text",
"value": "",
"required": true,
"order": 2
},
{
"elementType": "dropdown",
"key": 'project',
"label": 'Project Rating',
"options": [
{ "key": 'average', "value": 'Average' },
{ "key": 'good', "value": 'Good' },
{ "key": 'great', "value": 'Great' }
],
"order": 3
}
];
在下拉列表中,我想从服务调用中获得 options
数组..
截至目前,您可以看到我已将其硬编码在选项中..
dynamic-form.component.ts
中的服务:
getDropdownValues(url,token) {
this.service.get(url,token).subscribe(res => {
console.log(res.data);
});
}
res.datareturns以下,
{
data: [
{ "key": 'average', "value": 'Average' },
{ "key": 'good', "value": 'Good' },
{ "key": 'great', "value": 'Great' }
]
}
此数据数组将成为 JSON 中的 options
..
截至目前,我已经在 .ts
文件中提供了 JSON,但稍后它将成为一个单独的 .json
文件。
working stackblitz:https://stackblitz.com/edit/angular-x4a5b6-ng8m4z
请帮助我将来自服务 (res.data) 的数据放入 JSON..
内的下拉列表 options
你必须问问自己你收到了什么数据,你需要什么数据。一般来说,你可以有,例如
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",..}
])
}
您必须使用 switchMap 来接收 fullModel。 switchmap 使您不会收到第一个电话,否则会收到内部电话。是一种不连接 subscribe
的方法
getFullMode():Observable<any[]>
{
return getOptions().pipe(switchMap(
opts=>{
return getModel().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
}))
})
)
}
好吧,你的情况更简单,因为只有一个 "option" 和一个唯一的 "option",并且 json 是固定的。
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.data
return this.jsonData
}))
}).subscribe(res=>console.log(res));
如果你的 json 来自 observable,不要使用 map,使用 switchmap。 SwitchMap,等待外层调用结束再做第二个
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)
return this.service.getJsonData(pipe(map(jsonData=>{
let element=jsonData.find(e=>e.elementType=='dropdown');
if (element)
element.option=res.data
return this.jsonData
}))
}).subscribe(res=>console.log(res));
我正在制作 angular 6 应用程序,我正在制作一个 angular 动态表单,其中数据来自 来自 JSON.
JSON:
jsonData: any = [
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_name",
"label": "Project Name",
"type": "text",
"value": "",
"required": false,
"minlength": 3,
"maxlength": 20,
"order": 1
},
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_desc",
"label": "Project Description",
"type": "text",
"value": "",
"required": true,
"order": 2
},
{
"elementType": "dropdown",
"key": 'project',
"label": 'Project Rating',
"options": [
{ "key": 'average', "value": 'Average' },
{ "key": 'good', "value": 'Good' },
{ "key": 'great', "value": 'Great' }
],
"order": 3
}
];
在下拉列表中,我想从服务调用中获得 options
数组..
截至目前,您可以看到我已将其硬编码在选项中..
dynamic-form.component.ts
中的服务:
getDropdownValues(url,token) {
this.service.get(url,token).subscribe(res => {
console.log(res.data);
});
}
res.datareturns以下,
{
data: [
{ "key": 'average', "value": 'Average' },
{ "key": 'good', "value": 'Good' },
{ "key": 'great', "value": 'Great' }
]
}
此数据数组将成为 JSON 中的 options
..
截至目前,我已经在 .ts
文件中提供了 JSON,但稍后它将成为一个单独的 .json
文件。
working stackblitz:https://stackblitz.com/edit/angular-x4a5b6-ng8m4z
请帮助我将来自服务 (res.data) 的数据放入 JSON..
内的下拉列表options
你必须问问自己你收到了什么数据,你需要什么数据。一般来说,你可以有,例如
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",..}
])
}
您必须使用 switchMap 来接收 fullModel。 switchmap 使您不会收到第一个电话,否则会收到内部电话。是一种不连接 subscribe
的方法getFullMode():Observable<any[]>
{
return getOptions().pipe(switchMap(
opts=>{
return getModel().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
}))
})
)
}
好吧,你的情况更简单,因为只有一个 "option" 和一个唯一的 "option",并且 json 是固定的。
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.data
return this.jsonData
}))
}).subscribe(res=>console.log(res));
如果你的 json 来自 observable,不要使用 map,使用 switchmap。 SwitchMap,等待外层调用结束再做第二个
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)
return this.service.getJsonData(pipe(map(jsonData=>{
let element=jsonData.find(e=>e.elementType=='dropdown');
if (element)
element.option=res.data
return this.jsonData
}))
}).subscribe(res=>console.log(res));