gpt4 book ai didi

angular - 测试 FormArray

转载 作者:太空狗 更新时间:2023-10-29 17:51:45 25 4
gpt4 key购买 nike

我有一个 PhoneNumbersFormComponent 其模板如下所示:

<div [formGroup]="form">
<div formArrayName="numbers">
<input formControlName="countryPrefix">
<input formControlName="number">
</div>
</div>

我想通过 angular-cli 的 ng g component xxx 创建的默认测试

我得到的第一个错误是:

Cannot find control with name 'countryPrefix'

我解决了这个问题:

  beforeEach(() => {
fixture = TestBed.createComponent(PhoneNumbersFormComponent);
component = fixture.componentInstance;
component.phoneNumbers = [];
component.form = new FormGroup({
countryPrefix: new FormControl(),
number: new FormControl()
});
fixture.detectChanges();
});

现在剩下的最后一个错误是:

Cannot find control with name 'numbers'

而且我不知道如何测试 formArrayName="numbers"

最佳答案

尝试这样的事情:

let array: FormGroup[] = [];
array.push(new FormGroup({
countryPrefix: new FormControl(),
number: new FormControl()
}));
let formArray = new FormArray(array);
component.form = new FormGroup({
numbers: formArray // or array not totally sure
})

关于angular - 测试 FormArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46723685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com