繁体   English   中英

如何在Angular的FormArray反应形式中获得唯一值?

[英]How to get unique value in FormArray reactive form in Angular?

我创建了一个stackblitz应用程序,以在此处演示我的问题: https ://angular-ry1vc1.stackblitz.io/

我在选择HTML列表上有formArray值。 每当用户更改选择时,它都应在页面上显示选择的值。 问题是我如何只显示当前选择,而不能覆盖以前选择的值。 我想知道如何使用key来使值的占位符唯一。 TIA

form.html

<form [formGroup]="heroForm" novalidate class="form">
  <section formArrayName="league" class="col-md-12">
    <h3>Heroes</h3>
    <div class="form-inline" *ngFor="let h of heroForm.controls.league.controls; let i = index" [formGroupName]="i">
      <label>Name</label>
      <select (change)="onSelectingHero($event.target.value)">
        <option *ngFor="let hero of heroes" [value]="hero.id" class="form-control">{{hero.name}}</option>
      </select> <hr />
      <div>
        Hero detail: {{selectedHero.name}}
      </div> <hr />
    </div>
    <button (click)="addMoreHeroes()" class="btn btn-sm btn-primary">Add more heroes</button>
  </section>
</form>

component.ts

import { Component, OnInit } from '@angular/core';
import { FormArray, FormBuilder, FormControl, FormGroup, Validators, NgForm } from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit  {
  heroes = [];
  heroForm: FormGroup;
  selectedHero;

  constructor(
    private fb: FormBuilder,
  ) {
    this.heroForm = fb.group({
      league: fb.array([
        this.loadHeroes(),
        this.loadHeroes(),
        this.loadHeroes()
      ])
    });
  }

  ngOnInit() {
    this.listHeroes();
  }

  public addMoreHeroes() {
    const control = this.heroForm.get('league') as FormArray;
    control.push(this.loadHeroes());
  }

  public loadHeroes() {
    return this.fb.group(
      {
        id: this.heroes[0],
        name: '',
        level: '',
        skill: '',
      }
    );
  }  

  public listHeroes() {
    this.heroes = [
      {
        id: 1,
        name: 'Superman'
      },
      {
        id: 2,
        name: 'Batman'
      },
      {
        id: 3,
        name: 'Aquaman'
      },
      {
        id: 4,
        name: 'Wonderwoman'
      }      
    ];
  }

  public onSelectingHero(heroId) {
    this.heroes.forEach((hero) => {
      if(hero.id === +heroId) {
        this.selectedHero = hero;
      }
    });
  }
}

如果这样做的目的是按数组元素仅显示选定的英雄,而不是替换所有选定的值,那么您可以使用数组表单元素获得一些帮助。

onSeletingHeroselectedHero不是必要的,我所取代,使用通过表单值formControlName属性,在该示例中, id控制是select h.value.id是获取所选值ID的方法。

    <select formControlName="id">
        <option *ngFor="let hero of heroes;" [value]="hero.id" class="form-control">{{hero.name}}</option>
      </select> <hr />
      <div>
        Hero detail: {{getHeroById(h.value.id).name}}
      </div> <hr />
    </div>

B.为了获得选中的英雄,我添加了getHeroById方法。

  getHeroById(id: number) {
    return id ? this.heroes.find(x => x.id === +id) : {id: 0, name: ''};
  }

希望这些信息能解决您的问题。 干杯

暂无
暂无

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

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