javascript - 如何在formArray中推送字符串数组?
问题描述
我需要创建这个:
instructions: ['value one' , 'value two' , 'value three'];
我正在使用 formArray 并首先在 stackblitz 上检查我的代码: https ://stackblitz.com/edit/formarray-obj?file=app/app.component.ts
html:
<div class="form">
<form id="form" [formGroup]="filmForm" (ngSubmit)="save()">
<button type="button" (click)="addItem()"> add new row</button>
<div formArrayName="instructions" *ngFor="let a of filmForm.get('instructions').controls; let i = index">
<div [formGroupName]="i" style="margin-bottom: 10px;">
<label for="name">Name:</label>
<input type="text" name="name" formControlName="instructions">
<br><br>
</div>
</div>
<div class="submit">
<button type="submit">Save</button>
</div>
</form>
</div>
TS:
registerForm: FormGroup;
submitted = false;
instructions: FormArray;
filmForm: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.filmForm = this.formBuilder.group({
instructions: this.formBuilder.array([this.createItem()])
})
}
createItem() {
return this.formBuilder.group({
instructions: ['']
})
}
addItem() {
this.instructions = this.filmForm.get('instructions') as FormArray;
this.instructions.push(this.createItem());
}
save() {
console.log(this.filmForm.value)
}
我正在发送对象数组:检查 stackblitz console.log 我发送喜欢
instructors = [{instructs : 'value one'} , {instructs : 'value one'}..]
但我需要发送:
instructors = ["value one" , "value two"];
解决方案
你可以试试这个..我有同样的问题。
制作表格
form = new FormGroup({
instructorsArray: new FormArray([]),
})
get instructors(): FormGroup {
return FormControl('')
}
addInstructors() {
this.instructors.push(this.instructors);
}
推荐阅读
- jenkins - 在 Jenkins 中执行作业时,仅针对特定存储库的 Jfrog 握手问题
- c++ - 如何在 Mac 上设置/更新 cpp
- http - 无法使用上传到 cloudinary 的公共 url 访问 pdf
- powershell - 使用powershell在特定行中写入txt文件
- java - 协调 maven 和 java 之间的 java 版本
- python - 在循环中随机化字符
- python - 单元测试练习的问题,未找到模块
- amazon-web-services - 在terraform中获取元组的第一个元素
- java - spring-boot-configuration-processor 无法为嵌套类生成元数据
- c# - ValidationResult 发送 409 冲突状态码 ASP.NET Core Web Api