测试FormArray [英] Testing FormArray

查看:60
本文介绍了测试FormArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a PhoneNumbersFormComponent whose template looks like :

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

我想通过angular-cli的ng g component xxx

I want to pass the default test created by angular-cli's ng g component xxx

我遇到的第一个错误是:

The first error I got was:

找不到名称为"countryPrefix"的控件

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();
  });

现在最后剩下的错误是:

Now the last remaining error is:

找不到名称为数字"的控件

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
})

这篇关于测试FormArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆