【问题标题】:Problem with input field in a FormGroup in ionic/angular离子/角度中 FormGroup 中的输入字段存在问题
【发布时间】:2020-07-10 10:01:26
【问题描述】:

我对 FormGroup 有疑问。我有一个地点描述列表,用于填写带有相应离子输入字段的表格。用户应该能够更新描述并提交它。 但是当我不更新字段中的值时,发送到ts文件的数据是空的,即使它在屏幕上显示得很好。你能告诉我如何解决它吗?

这是我的 html 文件:

<ion-content padding class="form-content" [fullscreen]="true">
    <ion-header collapse="condense">
      <ion-toolbar>
        <ion-title size="large">Debug</ion-title>
      </ion-toolbar>
    </ion-header>
  <form [formGroup]="validations_form"  (ngSubmit)="onSubmit(validations_form.value)">    
    <div *ngFor="let place of (places | async)">
      <ion-item>
        <ion-label color="primary"></ion-label>
        <ion-input type="text" formControlName="desc_{{place.id}}" [value]="place.name"></ion-input>
      </ion-item>
    </div>
    <ion-button expand="full" type="submit">Save</ion-button>
  </form>
</ion-content>

这是我的 ts:

import { Component} from '@angular/core';
import { Observable } from 'rxjs';
import { Validators, FormBuilder, FormGroup, FormControl, FormArray, ValidationErrors } from '@angular/forms';
import { PersonService } from '../../person.service';
import Place from '../../types';


@Component({
  selector: 'app-debug',
  templateUrl: './debug.page.html',
  styleUrls: ['./debug.page.scss'],
})
export class DebugPage {//} implements OnInit {
  places: Observable<Place[]>;
  validations_form: FormGroup;
  constructor(
    public formBuilder: FormBuilder,
    public personService: PersonService
  ) {
    this.places = personService.getAllPlaces_full(); 
    this.validations_form = this.formBuilder.group({
      'desc_1': ['', Validators.required],
      'desc_2': ['', Validators.required],
      'desc_3': ['', Validators.required],
      'desc_4': ['', Validators.required],
      'desc_5': ['', Validators.required],
      'desc_6': ['', Validators.required]});
   }

onSubmit(values){ 
    console.log(values);
    for (let [key, value] of Object.entries(values)) {
      if (!( value ===null))
      {
        console.log(`${key}: ${value}`);  
      }
    }
  }
}

这是屏幕: the screen

这是日志: the log in the console

【问题讨论】:

  • 您不能在 ReactiveForm 中使用[value],您需要使用 setValue、pathValue 或在创建表单时明确地为 ReactiveForms 的控件赋值。在您的情况下,您可以使用 pipe(tap) 为表单赋予价值
  • 非常感谢@Eliseo,我就是这样做的...如果您提出它作为答案,我会接受它..

标签: angular ionic-framework formgroups


【解决方案1】:

用于填充表单的模型是:validations_form。 这完全独立于可观察的位置,您必须使用可观察的作为模型,并且通过使用 RXJS,您可以轻松地从可观察的读取数据 HTML:

<ion-content padding class="form-content" [fullscreen]="true">
    <ion-header collapse="condense">
      <ion-toolbar>
        <ion-title size="large">Debug</ion-title>
      </ion-toolbar>
    </ion-header>
  <form [formGroup]="places | async as Places"  (ngSubmit)="onSubmit()">    
    <div *ngFor="let place of Places">
      <ion-item>
        <ion-label color="primary"></ion-label>
        <ion-input type="text" formControlName="desc_{{place.id}}" [value]="place.name"></ion-input>
      </ion-item>
    </div>
    <ion-button expand="full" type="submit">Save</ion-button>
  </form>
</ion-content>

TS:

onSubmit(){ 

   places.subscribe(res => {
    console.log(res);
    // do something with your data 
    });
  }

【讨论】:

  • 感谢您回复@JoaVer,但它与我的解释不符...可能不够清楚...
猜你喜欢
  • 2019-10-02
  • 2019-02-07
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
相关资源
最近更新 更多