将输入的值保存在对象Angular 8中

时间:2020-06-11 23:22:39

标签: typescript forms object angular8

我正在尝试将某些输入的值保存在对象中,但是我不知道该怎么做

enter image description here

此刻我可以正确地从表单中获取值,我要做的是将“ animalSelected0” “ animalSelected1” 存储在另一个对象中就像_proto

enter image description here

模板

<form #geographicalAreaForm="ngForm" (ngSubmit)="addGeographicalArea()">
      <div class="form-row">
          <div class="form-group col-md-6" *ngFor="let animal of animalService.animals; index as i">
            <label for="{{'animalSelected'+ i}}">Animal asigando</label>
            <input type="text" class="form-control" id="{{'animalSelected'+ i}}" name="{{'animalSelected'+ i}}"
              #animalName='ngModel'
              [(ngModel)]="animal.name" placeholder="{{animal.name}}" readonly>
          </div>
        </div>
          <div class="mt-3">
            <button type="button" class="btn btn-success mr-2"
              (click)="addGeographicalArea(geographicalAreaForm)">Guardar</button>
            <button type="button" class="btn btn-primary" (click)="resetForm(geographicalAreaForm)">Limpiar</button>
          </div>
        </form>

组件

addGeographicalArea(form?: NgForm) {
    if (form.value._id) {
    this.geographicalAreaService.putGeographicalArea(form.value)
      .subscribe(res => {
        this.getGeographicalArea();
        this.resetForm(form);
      });
    } else {
      this.geographicalAreaService.postGeographicalArea(form.value)
        .subscribe(res => {
          this.getGeographicalArea();
          this.resetForm(form);
        });
    }
  }

0 个答案:

没有答案