当Angular中的按钮在FormArray内不起作用时,可能有以下几种解决方法:
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.css']
})
export class FormComponent {
form: FormGroup;
constructor(private fb: FormBuilder) {
this.form = this.fb.group({
items: this.fb.array([])
});
}
get items(): FormArray {
return this.form.get('items') as FormArray;
}
addItem() {
this.items.push(this.fb.control(''));
}
removeItem(index: number) {
this.items.removeAt(index);
}
}
addItem() {
const item = new FormControl('');
this.items.push(item);
}
removeItem(index: number) {
this.items.removeAt(index);
}
这些方法可以帮助解决Angular中按钮在FormArray内不起作用的问题。请根据你的具体情况选择适合的解决方法。