html - 如何在 Angular 7 中从 JSON 生成 HTML FORM

标签 html json angular forms angular7

我想从 JSON 下面生成一个 HTML 表单。

{  
   "templateName":"C-Learn Survey",
   "surveyQuestions":[  
      {  
         "questionTitle":"Enter your name",
         "questionType":"Text",
         "questionGroup":{  

         }
      },
      {  
         "questionTitle":"Enter your empid:",
         "questionType":"Text",
         "questionGroup":{  

         }
      },
      {  
         "questionTitle":"Select your technologies",
         "questionType":"Multi choice",
         "questionGroup":{  
            "options":[  
               {  
                  "optionText":"Java"
               },
               {  
                  "optionText":"Mule"
               },
               {  
                  "optionText":".net"
               }
            ],
            "showRemarksBox":false
         }
      },
      {  
         "questionTitle":"Gender",
         "questionType":"Single choice",
         "questionGroup":{  
            "options":[  
               {  
                  "optionText":"Male"
               },
               {  
                  "optionText":"Female"
               }
            ],
            "showRemarksBox":false
         }
      }
   ]
}

例如

{  
             "questionTitle":"Enter your name",
             "questionType":"Text",
             "questionGroup":{  

             }

对于上面的 json 应该有一个像这样的 html 表单元素

<label>Enter your name</label>
<input type="text" placeholder="Enter your name"> 

我是 Angular 的新手,在互联网上看到一些帖子和代码说可以通过 jquery 请建议我如何实现。我正在使用 Angular 7。

最佳答案

试试这个 Angular6-json-schema-form

Stackblitz example

在你的 typescript 文件中

import { jsonSchema } from './jsonSchema';

jsonFormOptions = {
    loadExternalAssets: false,
  };
  schema = {
    "type": "object",
    "properties": {
      "first_name": { "type": "string" },
      "last_name": { "type": "string" },
      "address": {
        "type": "object",
        "properties": {
          "street_1": { "type": "string" },
          "street_2": { "type": "string" },
          "city": { "type": "string" },
          "state": {
            "type": "string",
            "enum": [ "AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", "DE",
                "DC", "FM", "FL", "GA", "GU", "HI", "ID", "IL", "IN", "IA",
                "KS", "KY", "LA", "ME", "MH", "MD", "MA", "MI", "MN", "MS",
                "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND",
                "MP", "OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", "SD",
                "TN", "TX", "UT", "VT", "VI", "VA", "WA", "WV", "WI", "WY" ]
          },
          "zip_code": { "type": "string" }
        }
      },
      "birthday": { "type": "string" },
      "notes": { "type": "string" },
      "phone_numbers": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "type": { "type": "string", "enum": [ "cell", "home", "work" ] },
            "number": { "type": "string" }
          },
          "required": [ "type", "number" ]
        }
      }
    },
    "required": [ "last_name" ]
  };

在你的 html 文件中

<json-schema-form
  [schema]="schema"
  [layout]="layout"
  [options]='jsonFormOptions'
  [framework]="'bootstrap-4'"
  (onSubmit)="onSubmit($event)"
  (formSchema)="showFormSchemaFn($event)"
  (formLayout)="showFormLayoutFn($event)">
</json-schema-form>

关于html - 如何在 Angular 7 中从 JSON 生成 HTML FORM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58760464/

相关文章:

json.map 不是函数

angular - ngx-image-zoom 设置最大宽度和高度

html - 如何将 float 子 div 的高度扩展到父级的高度?

javascript - 如何制作一个可读写的弹出窗口?

html - 使绝对定位的 child 随着他们的上级动态调整大小

json - Ansible:循环文件以执行 POST 请求

android - 获取 Instagram 快拍 Json

javascript - Angular-in-memory-web-api 中多个集合出现错误 404

Angular 7 : Sending post request gives error 405 (Method not allowed)

javascript - ng-disabled 是否在 div 标签中起作用?如果没有,那为什么?