我在Angular中创建了一个可重用的组件,我用它来构建多个表单,以便快速修改多个数据项 .
我在这个组件中有2个输入字段,当我在使用* ngFor的数组上进行迭代时,我通过输入属性进行绑定 . 一切都很好,并且可以很好地使用ID并显示数据,但我遇到的问题是当我推动更新这两个字段的值时显示为'undefined'(除非我修改它们) .
我的标记是:
<input
#catTitle
matInput
placeholder="Document Title"
[value]="categoryTitle"
formControlName="categoryTitle"
[errorStateMatcher]="matcher"
>
我的后端是:
categoryForm = new FormGroup({
categoryTitle: new FormControl('', [Validators.minLength(3), Validators.maxLength(50)]),
categoryDescription: new FormControl('', [Validators.minLength(5), Validators.maxLength(200)])
})
我确定我错过了一些非常愚蠢的东西 .
1 回答
VI很快就意识到在我的代码中我将Form Control的默认值设置为空白:
categoryTitle: new FormControl(''
并且已经修改为在那里修改变量并通过在视图init之后创建表单来确保它为's not set as ' null .