小编Sve*_*sen的帖子

Angular FormArray setValue 与来自服务的数据

我在 FormArray 方面遇到问题,可能需要一些帮助。\n我有一个带有变量 FormArray 的表单,它可以工作,我可以将数据发送到后端。\n问题是,我无法设置从我收到的数据中的值后端。

\n

这是打字稿代码

\n
this.form = this.fb.group({\n  title: new FormControl("",[Validators.required]),\n  actors: new FormArray([])\n})\n\nthis.moviesService.getMovie(this.movieId).subscribe(movieData => {\n  this.movie = movieData;\n  this.form.patchValue({\n    title: this.movie.title,\n    actors: this.movie.actors,           \n  })\n})\n
Run Code Online (Sandbox Code Playgroud)\n

然后在 html 中单击按钮,我调用此函数

\n
addActor(){\n  const actorsForm = this.fb.group({\n    actor: \'\',\n    role: \'\'\n  })\n  this.actors.push(actorsForm);\n}\n\nremoveActor(i: number){\n  this.actors.removeAt(i);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

和 HTML:

\n
<form [formGroup]="form" (submit)="onSubmit()">\n  <table  formArrayName="actors">\n    <tr>\n      <th colspan="2">Besetzung:</th>\n      <th width="150px">\n        <button type="button" mat-stroked-button color="primary" (click)="addActor()">Hinzuf\xc3\xbcgen +</button>\n      </th>\n    </tr>\n    <tr *ngFor="let actor of form.get(\'actors\')[\'controls\']; let i=index" [formGroupName]="i">\n      <td>\n …
Run Code Online (Sandbox Code Playgroud)

typescript angular angular-reactive-forms formarray

3
推荐指数
1
解决办法
1万
查看次数