简体   繁体   English

将复选框值传递给 Angular 中的数组

[英]Passing checkbox value to array in Angular

I'm working on a solution where each of the inputs, generated depending on the passenger number, gets additional checkboxes.我正在研究一种解决方案,其中根据乘客编号生成的每个输入都会获得额外的复选框。 I want to pass the value of each checkbox and bind it to the input next to it.我想传递每个复选框的值并将其绑定到它旁边的输入。 The plan is to eventually end up with an array in which I have the value of the input field + each checkbox value.计划最终得到一个数组,其中我有输入字段的值 + 每个复选框的值。 I am not sure how to move forward with this.我不确定如何继续前进。 For now, I have:目前,我有:

COMPONENT.HTML:组件.HTML:

<li *ngFor="let passenger of createRange(showStorage.passengersNumber); let i=index"><input type="text" [(ngModel)]="passengersData[i]" placeholder="Name and surname">
<input type="checkbox" (click)="addChild($event)">Child
<input type="checkbox" (click)="addLuggage($event)">Luggage
</li>

COMPONENT.TS组件.TS

  constructor() {
    this.passengersData = [];
  }

  public passengersData: any[];
  public luggageCounter: number = 0;
  public childrenCounter: number = 0;

  createRange(number){
  var items: number[] = [];
  for(var i = 1; i <= number; i++){
     items.push(i);
  }
  
  return items;
}

  addLuggage(event) {
    console.log("Added luggage");
    this.luggageCounter++
  }

  addChild(event) {
    this.childrenCounter++
    console.log(event);
  }

Thanks for any help!谢谢你的帮助!

STACKBLITZ: https://stackblitz.com/edit/flight-date-picker-with-service-done-qfmn6p STACKBLITZ: https://stackblitz.com/edit/flight-date-picker-with-service-done-qfmn6p

Use an array with a name property and boolean properties for child and luggage and regenerate the array only when the number of passengers is updated retaining the previous passengers.对儿童和行李使用具有 name 属性和 boolean 属性的数组,并仅在更新乘客数量并保留先前乘客的情况下重新生成数组。

Here is a working StackBlitz https://stackblitz.com/edit/angular-ivy-s7dtu4?file=src%2Fapp%2Fapp.component.ts这是一个有效的 StackBlitz https://stackblitz.com/edit/angular-ivy-s7dtu4?file=src%2Fapp%2Fapp.component.ts

Number of passengers <input type="text" [ngModel]="passengers.length" (ngModelChange)="updateNumOfPassengers($event)">
<li *ngFor="let passenger of passengers; let i=index">
  <input type="text" [name]="'name_' + 1" [(ngModel)]="passenger.name" placeholder="Name and surname">
  <input type="checkbox" [checked]="passenger.child" (change)="passenger.child = !passenger.child">Child
  <input type="checkbox" [checked]="passenger.luggage" (change)="passenger.luggage = ! passenger.luggage">Luggage
</li>
  passengers = [];

  updateNumOfPassengers(input: string) {
    const numOfPassengers = parseInt(input);
    if (!isNaN(numOfPassengers) && numOfPassengers >= 0) {
      const passengersToAdd = numOfPassengers - this.passengers.length;
      if (passengersToAdd > 0) {
        this.passengers = [...this.passengers, ...Array.from({ length: passengersToAdd }, _ => ({ name: '', child: false, luggage: false }))];
      } else if (passengersToAdd < 0) {
        this.passengers = this.passengers.filter((_, index) => index < numOfPassengers);
      }
    }
  }

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

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