【问题标题】:How to assign data from service to JSON?如何将服务中的数据分配给 JSON?
【发布时间】:2019-04-15 02:49:29
【问题描述】:

我正在制作 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.data 返回以下内容,

  {
    data: [
        { "key": 'average', "value": 'Average' },
        { "key": 'good', "value": 'Good' },
        { "key": 'great', "value": 'Great' }
    ]
  }

这个数据数组将是 JSON 格式的 options..

到目前为止,我已经在 .ts 文件中提供了 JSON,但稍后它将是一个单独的 .json 文件。

工作中的堆栈闪电战https://stackblitz.com/edit/angular-x4a5b6-ng8m4z

请帮助我将来自服务 (res.data) 的数据放置到 JSON 中的下拉列表 options..

【问题讨论】:

  • 目前,jsonData 是一个数组。 options 是否总是在第三个对象中,还是需要寻找它?
  • @user184994, 没有订单可能随时更改.. 一切都会随时更改,但对于选项数组,将放置 res.data 值..
  • @Nik,是的,它绝对会与下拉菜单一起使用..
  • 可以使用jsonData.find(d => d.elementType === "dropdown").optionsjsonData获取options数组
  • @user184994,我是 Angular 的新手。如果你用 stackblitz 将它作为答案来详细说明,这对我会有帮助。

标签: javascript json angular typescript angular-reactive-forms


【解决方案1】:

您必须问问自己您收到了哪些数据以及您需要哪些数据。一般来说,你可以有,例如

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 使您不会收到第一个电话,否则是内部电话。是一种不连接订阅的方法

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
           }))
        })
     )
}

好吧,你的情况更简单,因为只有一个“选项”和一个唯一的“选项”,anf 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));

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 2015-03-04
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多