Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.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
在Angular 8中创建动态文本框,并在表单提交时将值绑定到组件_Angular_Typescript - Fatal编程技术网

在Angular 8中创建动态文本框,并在表单提交时将值绑定到组件

在Angular 8中创建动态文本框,并在表单提交时将值绑定到组件,angular,typescript,Angular,Typescript,我试图在按下按钮时添加动态文本框。动态文本框已成功添加,但在表单提交时无法获取所有值(动态添加的文本框值)。它始终绑定上次更新的文本框值。下面是我的代码片段 贡献.component.ts export class ContributeComponent implements OnInit { categories = []; contribute = {}; private options = [{ value: '' }]; private contributeForm: FormGroup

我试图在按下按钮时添加动态文本框。动态文本框已成功添加,但在表单提交时无法获取所有值(动态添加的文本框值)。它始终绑定上次更新的文本框值。下面是我的代码片段

贡献.component.ts

export class ContributeComponent implements OnInit {
categories = [];
contribute = {};
private options = [{ value: '' }];
private contributeForm: FormGroup;
constructor(private formBuilder: FormBuilder) {}

ngOnInit() {
this.contributeForm = this.formBuilder.group({
  question: new FormControl(),
  category: new FormControl(),
  options: new FormControl()
});
add() {
this.options.push({ value: this.contributeForm.controls.options.value });
}
在我的html文件contribute.component.html中

<form [formGroup]="contributeForm" (ngSubmit)="onSubmit(contributeForm.value)">
<div class="form-group">
                <label class="control-label col-sm-2" for="message">Options:</label>
                <div class="col-sm-10 inputGroupContainer">
                    <div *ngFor="let option of options; let in=index" class="spacer">
                        <div class="input-group">
                            <span class="input-group-addon">Option {{in+1}}</span>
                            <input type="text" class="form-control" rows="4" placeholder="Type your option here"
                                formControlName="options" name="option" [value]="options[in].value"/>
                        </div>
                    </div>
                </div>

                <div class="row"></div>

                <div class="col-sm-12 pull-right">
                    <div class="col-sm-4"></div>
                    <div class="col-sm-4">
                        <button class="btn btn-warning" type="button" (click)="add()">Add option <span
                                class="glyphicon glyphicon-send"></span>
                        </button>
                    </div>
                    <div class="col-sm-4"></div>
                </div>
            </div>

选项:
选项{{in+1}}
添加选项

我想你对这应该如何工作感到困惑。您的
contributeForm
只有一个
options
字段,因此您只能将一个值绑定到此字段……也许您希望将其设置为数组

每次调用
add()
函数时,它都会得到
选项的当前值<代码>选项
仅绑定到单个文本框,该文本框将是最后创建的文本框,即底部文本框。因此,每次添加新选项时,最新的选项文本框都将绑定到
options


每次添加新选项时,请查看StackBlitz和此控制台

我明白你的意思了,那么在提交表单时如何将每个值正确地放入数组中。我是Angular的新手,正在尝试找到解决方案