【发布时间】:2020-06-16 23:23:35
【问题描述】:
我需要创建这个:
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"];
【问题讨论】:
-
在 Angular v8 之前我不这么认为。 #关注
-
我不明白? v8 呢?
-
我的意思是直到 Angular 版本 8。
-
这个answer 可能对你有帮助
-
您可以使用 rxjs 来格式化您的数据:
console.log({ instructions: this.filmForm.value.instructions.map(e=> e.instructions) });。看看我的 stackblitz:stackblitz.com/edit/formarray-obj-kkszod
标签: javascript html angular