简体   繁体   中英

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.

For Example:

Component.ts

fb: FormBuilder;
formGroupData: FormGroup;

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

HTML:

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

This should create the entire form without adding the element selectedAmount and selectedItem elements. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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