简体   繁体   English

使用来自服务器的数据填充 Angular 的响应式表单

[英]Populating Angular's Reactive form with data from server

Trying to insert server data into Angular's form so that the FormGroup property key is that from the server data.尝试将服务器数据插入到 Angular 的表单中,以便 FormGroup 属性键是来自服务器数据的键。 Followed Angular's docs on dynamic forms but it wasn't exactly what i wanted ( https://angular.io/guide/reactive-forms#creating-dynamic-forms ).遵循 Angular 关于动态 forms 的文档,但这并不是我想要的( https://angular.io/guide/reactive-forms#creating-dynamic-forms )。

The point is to populate the form with the data and display it in template with *ngFor using sliders on each property to get a value.关键是用数据填充表单并在模板中显示它, *ngFor使用每个属性上的滑块来获取一个值。 Then sending the data back to server.然后将数据发送回服务器。

Example of the data object:数据 object 示例:

{
  "things": [
    {
      "thingId": 1,
      "thingName": "bear"
    },
    {
      "thingId": 2,
      "thingName": "piano"
    }
  ]
}

Service getting the data and assigning to variable:服务获取数据并分配给变量:

things: IThing[] = [];

getThings() {
  this.thingService.getThings()
    .subscribe(data => {
      this.things = data.things;
    });
}

I need to populate the things FormGroup (or FormArray if needed) with the values from the data object fetched from the server, that is, the thingName property value => "bear", "piano".我需要使用从服务器获取的数据 object 中的值填充things FormGroup(或 FormArray,如果需要),即thingName属性值 =>“熊”、“钢琴”。

ngOnInit() {
  this.myForm = this.fb.group({
    title: ['', []],
    comment: ['', []],

    // need to populate the things object here, example:
    things: this.fb.group([
      // bear: [,[]],
      // piano: [,[]]
      // and so on...
    ])
  })

  this.getThings();
}

You can create a empy FormGroup and use addControl to add a control with a property name您可以创建一个 empy FormGroup 并使用addControl添加具有属性名称的控件

things: this.fb.group({}) //<--a empty formGroup

And in the subscribe of the things并在订阅的东西

this.thingService.getThings()
    .subscribe(data => {
      this.things = data.things;
      this.things.forEach(x=>{
          (this.myForm.get('things') as FormGroup)
                 .addControl(x.thingName,this.fb.control(''))
      })
    });

NOTE: In.html you has some like注意:在.html 你有一些喜欢

<form [formGroup]="myForm">
  ...
  <div formGroupName="things">
      <div *ngFor="let thing of things">
          <input [formControlName]="thing.thingName">
      </div>
  </div>
</form>

Update Another thing is that you really want has an FormArray, you need think about you database (if you want to store in a database).更新另一件事是你真的想要一个FormArray,你需要考虑你的数据库(如果你想存储在数据库中)。 In this case you only need a FormArray of FormControls, so, at first you create a empty FormArray在这种情况下,您只需要 FormControls 的 FormArray,因此,首先您创建一个空的 FormArray

things: this.fb.array([]) //<--a empty formArray

And, in subscribe you add to your formArray so many controls as elements has the things而且,在订阅中,您向 formArray 添加了许多控件,因为元素具有这些东西

this.thingService.getThings()
    .subscribe(data => {
      this.things = data.things;
      this.things.forEach(x=>{
          (this.myForm.get('things') as FormArray)
                 .push(this.fb.control(''))
      })
    });

And the.html like和.html之类的

<form [formGroup]="myForm">
  ...
  <div formArrayName="things">
      <div *ngFor="let controls of thingsArray.controls;let i=index">
          {{things[i].thingName}}:<input [formControlName]="i">
      </div>
  </div>
</form>

Use a getter to get the array使用 getter 获取数组

get thingsArray()
  {
    return this.myForm.get('things') as FormArray
  }

I put both cases in the stackblitz -in the stackblitz you has myForm and myForm2-我把这两种情况都放在了stackblitz中——在stackblitz中你有myForm和myForm2-

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

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