- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试构建与 FormGroup 和 FormArray 兼容的数据表。我将使用 parentForm 组将标题详细信息和行传递给此组件。
我创建了一个小的添加按钮,点击它,我将动态添加新项目。在这种情况下,UI 可以正常更新,但如果我打印 formGroup.value(),我只会得到传递的初始值。不是更新的 formControl。我不确定我错过了什么。
export class FormTableComponent implements OnChanges {
@Input() headers: Array<string>;
@Input() rows: Array<any>;
@Input() parentFG: FormGroup;
@Input() entriesName: string;
get entries(): FormArray {
return <FormArray>this.parentFG.get(this.entriesName);
}
constructor(private fb: FormBuilder, private cd: ChangeDetectorRef) {}
ngOnChanges() {
this.createFormControls();
}
createFormControls() {
this.parentFG.controls[this.entriesName] = this.fb.array([]);
this.rows.forEach(row => {
this.entries.controls.push(this.fb.group(row));
});
}
}
最佳答案
您有几个问题:1. 将新行推送到 FormArray 本身以外的 FormArray 控件
this.entries.controls.push(this.fb.group(row));
应该是
this.entries.push(this.fb.group(row));
2。不要每次都创建新的 FormArray 并在调用 createFormControls()
时重新加载每一行,您可以通过 get entries()
方法访问它
解决方案一:
检查 entries
的长度,如果为 0,则用 rows
初始化它,否则将最后一行插入现有的 FormArray
createFormControls() {
const keys = Object.keys(this.entries.value);
if( keys.length === 0) {
this.rows.forEach(row => {
this.entries.push(this.fb.group(row));
});
} else {
const row = this.rows[this.rows.length - 1];
this.entries.push(this.fb.group(row));
}
}
解决方案 2:
就个人而言,我更喜欢将 createFormArray()
和 appendTo()
或 popFrom()
放入父组件中。它干净且易于阅读。在这种情况下,我们不需要 ChangeDetectionRef
。
我已将 createFormControls()
从 form-table
组件移至 dummy
组件并将其重命名为 createFormArray()
并稍微更改了 addNewRow()
,现在一切正常
// create FormArray once
createFormArray() {
const elements: any[] =[];
this.rows.forEach(row => {
elements.push(this.fb.group(row));
});
return this.fb.array(elements);
}
addNewRow() {
const newRow = {
name: 'NAFFD',
age: 12,
place: 'TN'
};
this.rows.push(newRow); // update rows
const persons = this.dummyForm.get('persons') as FormArray;
persons.push(this.fb.group(newRow)); // update FormArray
}
初始化虚拟表单时,请执行以下操作:
this.dummyForm = this.fb.group({
persons: this.createFormArray(),
store: this.fb.group({
branch: '',
code: ''
})
});
希望对您有所帮助,祝您编码愉快!
关于angular - 如何在 Angular 5 中将 FormControl 动态添加到 FormGroup?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48833791/
我有一个父组件,它是主要的 FormGroup .在此 组件我有一个子组件, . 子组件有一个 *ngIf="some-conditional" ,在子组件内部,我有一个快速填充按钮,它基本上是 pa
我找到了这个答案,它展示了如何将表单控件添加到父表单: Reuse components in angular2 model driven forms 但我希望能够像这样将新的 formGroup 添
我在 Plunkr 上有一个 Angular 2 RC4 基本表单示例,它似乎抛出以下错误(在 Chrome DEV 控制台中) 这是笨蛋 https://plnkr.co/edit/GtPDxw?p
这个问题在这里已经有了答案: How to use formControlName and deal with nested formGroup? (4 个答案) 关闭 3 年前。 我有这个用于 a
我正在尝试将 formGroup 添加到我的整体表单中,条件是在我的情况下特定字段属于某种类型。我添加此 formGroup 的函数如下所示: initSocial() { const
情况: 我正在尝试在我的 Ionic 2 应用程序中创建一个非常简单的登录表单。 无论我尝试什么,我都会不断收到此错误: formGroup expects a FormGroup instance.
在我的 Angular 2 组件中,我使用带有 18 个 AbstractControl 的 Formgroup。用户可以编辑从数据库加载的个人资料。我想添加一个 Reset 按钮来将表单重置为其原始
我是 Angular 2 的新手,无法解决这个问题即使在阅读了其他堆栈溢出答案之后也是如此。 我刚刚开始学习 Angular react 形式,想尝试第一个例子,但被卡住了。请帮忙。 这是 HTML
我在 Angular 4 中创建了一个 formGroup,其中用户和组织存储在对象中。现在我想使用这两个对象填充我的表单组。在我的 ts 中,我做了以下事情: createForm(user: an
我了解错误的根源,我正在测试的组件需要将 FormGroup 传递到它的 @Input() 表单:FormGroup。我只是不知道如何在测试此组件时传入一个。 当我调用 fixture.detectC
我不确定这是否是一个错误,几乎 90% 肯定不是,但我想知道发生这种情况背后的逻辑。 我有一个组件,假设我在组件初始化时初始化一个 FormGroup。 export class FooCompone
我有一个定义 FormGroup 的 Angular 组件,它有一个嵌套的 FormGroup 作为它的控件之一。 子 FormGroup 作为 @Input 参数传递给子 component,并且在
我正在 Angular 2 中创建一个表单。我的目标是从 API 获取数据并将其传递到表单中以进行编辑。但是,我遇到了这个错误: EXCEPTION: Uncaught (in promise): E
我有第二种形式。我需要填写来自银行的日期字段,一个 json 文件。在 populateFields 方法中,我得到一个包含将要填充的数据的参数。但是,它只填充itens的值,即数组内部的描述,它不执
选择验证时是否有任何首选方式 myForm.controls['name'].valid myForm.get('name').valid 因为两者似乎只是在句法上有所不同,但实现了相同的目标。 Na
我在使用 Angular 2 表单时遇到了一些问题,我在我的项目中成功地实现了一些表单,当我尝试添加这个表单时,我的 Angular Cli 出现了错误:类型“FormGroup”不可分配给类型“ty
我的情况很简单。有一个包含两个字段的 html 表单: 名字 姓氏 我希望在用户更改 firstName 时更改 lastName。我通过 Angular (8) 中可观察到的 valueChange
我有以下模板。我正在尝试掌握响应式表单,但遇到了问题。 First N
我目前正在使用 Angular 6 创建一个带有 Reactive Form 的 Accordion。我也在使用 primeNG 的 Accordion 模块。 基于对象(cuList),将动态创建F
在 StackOverflow 的其他示例中,有很多关于在 FormArrays 中使用 FormGroups 的问题。但我的问题恰恰相反。 FormArrays 有一个 push 方法,这使得很多事
我是一名优秀的程序员,十分优秀!