Karma jasmine Angular 2+使用模板驱动的表单动态创建表单输入在单元测试的生产错误中起作用
我试图创建一个动态表单,该表单由用户以前的选择生成和填充。用户选择的每个配置文件都包含一个配置数组,该数组循环使用以创建后续输入 配置对象看起来非常像这样:Karma jasmine Angular 2+使用模板驱动的表单动态创建表单输入在单元测试的生产错误中起作用,karma-jasmine,angular2-forms,Karma Jasmine,Angular2 Forms,我试图创建一个动态表单,该表单由用户以前的选择生成和填充。用户选择的每个配置文件都包含一个配置数组,该数组循环使用以创建后续输入 配置对象看起来非常像这样: { Description: "A name for your security control, can have a maximum of 64 characters" MaxLength: 64 Name: "Name" Pattern: "^(.{1,64})$" Typ
{
Description: "A name for your security control, can have a maximum of 64 characters"
MaxLength: 64
Name: "Name"
Pattern: "^(.{1,64})$"
Type: "text"
}
这一切在生产过程中都可以正常工作,但当我尝试运行单元测试时,会出现以下错误:
Error: If ngModel is used within a form tag, either the name attribute must be set or the form
control must be defined as 'standalone' in ngModelOptions.
我正在动态地设置一个名称,所以我不明白为什么这不起作用
<div class="sc-config-form__input" *ngFor="let config of profile.Config">
<label *ngIf="!config.Conditional || isConditionMet(config)" class="cloud-grey-label fillLabel" for="sc{{config.Name}}-text">{{config.Name}}</label>
<input *ngIf="(!config.Conditional || isConditionMet(config)) && (!config.Type || config.Type =='text')"
maxlength="{{config.MaxLength}}"
name="{{config.Name}}"
[ngModel]="config.Default"
type="text"
id="sc{{config.Name}}-text"
placeholder="{{config.Name}}"
pattern="{{(config.Pattern ? config.Pattern : '')}}"
title="{{config.Description}}"
[required]="config.Required"
/>
/>
我还尝试使用let I=index并将索引动态添加到name属性。这允许它通过测试,但是我的控件的名称是错误的,它在生产环境中无法工作,原因有很多
有没有更简单的方法来解决这个问题 您需要添加的输入标记内部
[ngModelOptions]="{standalone: true}"
这将解决您的问题