简体   繁体   English

角度2:我们可以从FormGroup创建表单吗?

[英]Angular 2: Can we create the Form from FormGroup?

I am developing a dynamic form where the form elements are modified as per user inputs. 我正在开发一种动态表单,其中可以根据用户输入修改表单元素。 Is it possible to generate the complete form by just supplying the FormGroup object to the <form></form> tag instead of manually creating each element which is present in the form group. 是否可以通过仅将FormGroup对象提供给<form></form>标记而不是手动创建表单组中存在的每个元素来生成完整的表单。

For Example: 例如:

Component.ts Component.ts

fb: FormBuilder;
formGroupData: FormGroup;

constructor() {
this.formGroupData= this.fb.group({
            selectedItem: ['', Validators.required],
            selectedAmount: ['', Validators.required]
        });
}

HTML: HTML:

<form [formGroup]="formGroupData"></form>

This should create the entire form without adding the element selectedAmount and selectedItem elements. 这应该创建整个表单,而不添加元素selectedAmount和selectedItem元素。 Is this possible? 这可能吗? If not why what's the harm here? 如果不是,为什么这里有什么害处?

They did not build forms to work like that. 他们没有建立像这样的表格。 It would make css challenging especially when adding <label> s, input prefixes and suffixes, and other view-side stuff. 这将使CSS更具挑战性,尤其是在添加<label> ,输入前缀和后缀以及其他视图方面的东西时。 Ultimately the goal was to separate the view and the model in this way. 最终目标是通过这种方式将视图和模型分离。

You might be able to create a directive like this using the Renderer class if you need to access the view-side stuff. 如果您需要访问视图端的内容,则可以使用Renderer类创建类似的指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM