简体   繁体   English

如何使用来自服务器的实时数据构建表单(使用 Angular 反应式 forms - 表单构建器)

[英]How to construct a form (using Angular reactive forms - form builder) with live data coming from the server

I spend the last 2 days trying different ways to get the data in the right order but I just couldn't, basically I get some data from the server and display it for the client plus some extra fields which the client need to fill out.我花了最后 2 天尝试不同的方法以正确的顺序获取数据,但我做不到,基本上我从服务器获取一些数据并将其显示给客户端以及客户端需要填写的一些额外字段。

This is my data这是我的数据

formConfiguration = { // it's dynamic !! coming back from the server!
    dateSupply: {
        name: "dateSupply",
        validators: [
            Validators.required,
        ]
    },    
    products: [
        { 
            name: "product_1",
            costPerUnit: 3,
            quantityPerCarton: 10,
            value: 0,
            validators: [
                Validators.required,
            ]
        },
        { 
            name: "product_2",
            costPerUnit: 4,
            quantityPerCarton: 20,
            value: 0,
            validators: [
                Validators.required,
            ]
        }
    ],
}

this is what I did这就是我所做的

createOrderForm(products) {

    const group = this.formBuilder.group({});

    for (let i = 0; i < products.length; i++) {

        const control = this.fb.control(products[i].value, products[i].validators || []);
        group.addControl(products[i].name, control);
    };

    return group;

};

this is what I need..这就是我需要的..

form structure:
       controls:
           dateSupply: FormControl,
           products: FormArray,
               0 : FormGroup
                   controls:
                       name: FormControl
                       costPerUnit: FormControl
                       ...
               1 : FormGroup
                   controls:
                       name: FormControl
                       costPerUnit: FormControl
                       ...

please help..请帮忙..

You must use FormArray.您必须使用 FormArray。 I made an example on stackblitz我在stackblitz上做了一个例子

this.form = this._fb.group({
  dateSupply: this._fb.control(formConfiguration.dateSupply.name),
  products: this._fb.array(
    formConfiguration.products.map(({ value, validators }) =>
      this._fb.group({
        name: this._fb.control(value, validators || [])
      }),
    ),
  ),
});

暂无
暂无

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

相关问题 响应式表单中的 Angular 表单组与表单控件和表单构建器 - Angular form group vs form control and form builder in Reactive Forms 如何使用嵌套表单数组将服务器响应映射到有角反应式表单 - How to map server response to angular Reactive Forms with nested form array 如何在不使用反应形式的情况下验证来自循环提交的复选框 - angular 8 - How to validate checkbox coming from loop on submit without using reactive form - angular 8 Angular 8:将表单数据从子 Output 发送到具有反应性 Forms 的父 - Angular 8: Send Form Data from Child Output to Parent with Reactive Forms 表单生成器angular 2-如何构造控件数组? - form builder angular 2 - How to construct an array of controls? 如何使用反应式表单将表单绑定到 Angular 6 中的模型? - How can I bind a form to a model in Angular 6 using reactive forms? 如何以角度(反应形式)从父组件形式迭代数组 - How to iterate an array from parent componenent form in angular (reactive forms) 从反应形式Angular 5中删除形式验证 - Remove form validation from reactive forms Angular 5 如何从 valueChanges 获取选定的对象,但仅使用 Angular 反应形式将对象 id 绑定到表单? - How to get selected object from valueChanges but only bind object id to form using Angular reactive forms? 如何使用反应式表单将静态数据提取到表单 - How to fetch the static data to the form using reactive forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM