angular - 获取formGroup中的表单id
问题描述
我正式使用 ngx 来构建表单。我需要的是在我拥有的一些自定义类型中访问表单 ID。基本上我需要访问formGroup 中的表单ID。
HTML
<div>
<form [formGroup]="form" [id]="formId">
<formly-form [form]="form" [model]="model" [fields]="fields" [options]="options"></formly-form>
</form>
</div>
.ts
formId = "unique_id_from_backend"
form = new FormGroup({});
输入类型组件
@Component({
selector: 'input-field',
templateUrl: './input.html',
})
export class InputFieldComponent extends FieldType implements OnInit {
ngOnInit(): void {
console.log(this.form);
// here i can access formGroup that i am passing in <form> but it seems id property is
// not there. Is there some way i can access form id?
}
}
解决方案
在自定义字段类型中,您可以访问字段和选项实例,您可以在其中获取 id、key...
对于您的用例,您可以依赖formState
,因此将其分配formId
给options.formState
:
form = new FormGroup({});
options = { formState: { id: "unique_id_from_backend" } }
export class InputFieldComponent extends FieldType implements OnInit {
ngOnInit(): void {
console.log(this.formState.id);
}
}
推荐阅读
- c# - 使用从同一接口 Console .Net Core 继承的类进行依赖注入
- kotlin - 如何更改或删除 spring data neo4j 中的对象关系目标?
- kendo-ui-angular2 - 对话服务内容中的换行符被忽略
- c# - 在日期、小时和分钟之间迭代
- node.js - 在第二个计划中制作节点检查文件
- powershell - 如果程序存在卸载
- ios - 如何将 UILabel 添加到 UICollectionView 控制器
- xml - XSLT:当“n”被赋予另一个属性的值时,重复输出值 n 次
- java - 如何在流中查找字符串数组的值
- heroku - 由于缺少导入程序,Keystone.js 应用程序在 Heroku 上启动失败?