Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于JSON对象动态创建表单_Javascript_Typescript_Angular_Ionic2 - Fatal编程技术网

Javascript 基于JSON对象动态创建表单

Javascript 基于JSON对象动态创建表单,javascript,typescript,angular,ionic2,Javascript,Typescript,Angular,Ionic2,我目前正在开发一个应用程序,允许用户回答有关自己的问题。我正在从API检索这些问题。现在,我想创建一个表单,将这些问题作为输入。我目前正在使用类似于此处描述的简单表单的内容: 现在,我想知道是否有可能根据API提供的问题创建此表单。我可以得到一个包含所有问题的JSON对象,但是是否可以创建一个for循环,用JSON对象中的问题填充表单?上面示例中的这部分代码给我的印象是,这样做是不可能的: this.loginForm = fb.group({ email: ["", Validators.

我目前正在开发一个应用程序,允许用户回答有关自己的问题。我正在从API检索这些问题。现在,我想创建一个表单,将这些问题作为输入。我目前正在使用类似于此处描述的简单表单的内容:

现在,我想知道是否有可能根据API提供的问题创建此表单。我可以得到一个包含所有问题的JSON对象,但是是否可以创建一个for循环,用JSON对象中的问题填充表单?上面示例中的这部分代码给我的印象是,这样做是不可能的:

this.loginForm = fb.group({
  email: ["", Validators.required],
  password: ["", Validators.required]
});
使用

使用
ngFor
循环浏览问题,并为每个问题创建
ngControl

ngControl
名称将设置为
Question1
Question2

<form #form="ngForm" (ngSubmit)="logForm(form.value)">
  <div *ngFor="#question of questionsList; #i = index">
    <label>{{question}}</label>
    <input type="text" [ngControl]="'Question' + i">
  </div>
 <button type="submit">Submit</button>
</form>

{{问题}
提交
logForm
方法,在相应的组件中,将获得包含键(
ngControl
名称)和值(相应的输入值)的对象使用

使用
ngFor
循环浏览问题,并为每个问题创建
ngControl

ngControl
名称将设置为
Question1
Question2

<form #form="ngForm" (ngSubmit)="logForm(form.value)">
  <div *ngFor="#question of questionsList; #i = index">
    <label>{{question}}</label>
    <input type="text" [ngControl]="'Question' + i">
  </div>
 <button type="submit">Submit</button>
</form>

{{问题}
提交
logForm
方法,在相应的组件中,将获得包含键(
ngControl
name)和值(相应的输入值)的对象