【问题标题】:Bind Array Data with the Reactive Form使用响应式表单绑定数组数据
【发布时间】:2020-05-22 12:34:18
【问题描述】:

在学习 Angular 时,我遇到了一个问题。

我有一个使用响应式方法的表单。

我有一个数组“模型”,其中包含每个“模型”的“价格”

我希望当我选择“模型”时,它应该给我它的“价格”,当我验证表单时,我会在 console.log (this.form.value )

这是我的 HTML:

 <form [formGroup]="factureForm" (ngSubmit)="onSubmitForm()">
  <select formControlName="model">
    <option *ngFor="let model of models">{{ model.model }}</option>
  </select>
  <select formControlName="price">
    <option *ngFor="let model of models">{{ model.price }}</option>
  </select>
  <button type="submit">Submit</button>
</form>

这是我的 TS:

import { Component, OnInit } from "@angular/core";
import { FormsModule, FormGroup, FormBuilder } from "@angular/forms";

@Component({
  selector: "app-relational-data",
  templateUrl: "./relational-data.component.html",
  styleUrls: ["./relational-data.component.css"],
})
export class RelationalDataComponent implements OnInit {
  factureForm: FormGroup;
  models = [
    {
      model: "Model 1",
      price: 20,
    },
    {
      model: "Model 2",
      price: 50,
    },
  ];

  constructor(private formBuilder: FormBuilder) {}

  ngOnInit() {
    this.initFactureForm();
  }

  initFactureForm() {
    this.factureForm = this.formBuilder.group({
      model: [""],
      price: [""],
    });
  }

  onSubmitForm() {
    const newFacture = this.factureForm.value;
    console.log(newFacture);
  }
}

我迷路了。 提前谢谢你:)

【问题讨论】:

    标签: angular angular-reactive-forms


    【解决方案1】:

    由于您需要在更改型号时更改价格,因此您可能需要在型号更改时设置价格。而且您也不需要下拉价格,因为它取决于型号。

    <form [formGroup]="factureForm" (ngSubmit)="onSubmitForm()">
      <select formControlName="model">
        <option value=''>Select</option>
        <option *ngFor="let model of models">{{model.model}}</option>
      </select>
      <input type="text" formControlName="price">
      <button type="submit">Submit</button>
    </form>
    
    initFactureForm() {
      this.factureForm = this.formBuilder.group({
        model: [""],
        price: [""],
      });
    
      // Look for changes to the model form-control
      this.factureForm.get('model').valueChanges.subscribe(newValue => {
        // newValue will be holding the 'model' attribute of the selected model
        // Searching the models array for the item with the selected model name
        const selectedModel = this.models.find(item => item.model === newValue);
        // If the item is found in the array,
        // then set the price of the model item to the price form-control.
        // If not found, set price to ''
        if (selectedModel) {
          this.factureForm.get('price').setValue(selectedModel.price);
        } else {
          this.factureForm.get('price').setValue('');
        }
      });
    }
    

    【讨论】:

    • 它的工作!您能否解释一下您在 TS 文件中使用的方法 (item => item.model === newValue) ?非常感谢您的帮助:)
    • 我在代码中添加了一些 cmets。看看是否有帮助。如果还有什么不清楚的地方,请告诉我。
    【解决方案2】:

    我认为[ngValue] 不见了。

    <form [formGroup]="factureForm" (ngSubmit)="onSubmitForm()">
      <select formControlName="model">
        <option *ngFor="let model of models" [ngValue]="model.model">{{ model.model }}</option>
      </select>
      <select formControlName="price">
        <option *ngFor="let model of models" [ngValue]="model.price">{{ model.price }}</option>
      </select>
      <button type="submit">Submit</button>
    </form>
    

    【讨论】:

    • 谢谢你的帮助,我试过了,但这不是我要找的:) 我正在寻找改变型号的价格:) 再次感谢你,我找到了使用@Nayak 解决方案的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多