将按钮的索引值传递给弹出组件

Pass index value of button to a popover component

我是 angular 的初学者,目前使用 angular 9 我的 project.I 有一个名为 play-game 的组件,它有一组按钮。

播放-game.component.html

<div class="container">
  <div [formGroup]="seatsFormGroup">
    <div formArrayName="seatsArray" *ngFor="let seat of seats;let idx = index" id="seat-{{idx}}" class="col-sm-4 ">
      <label  [ngbPopover]="popOver" popoverTitle="Pop title" class="table-player-seat-button" (click)="sit(idx)">{{seat.displayName? seat.displayName: "Sit "+ idx}}</label>
      <ng-template #popOver>
        <app-seat-modal></app-seat-modal>
      </ng-template>
    </div>
  </div>
</div>

我的弹出窗口有一个单独的组件,如下所示

座位-modal.component.html

<h4 class="modal-title">Seat{{seat}}</h4>
<button type="button" class="close" data-dismiss="modal" (click)="hide()">&times;</button>
<form [formGroup]="formRequestSeat" (submit)="join(seat)">
  <div>
    <button type="submit" class="button-request-seat">Request The Seat</button>
  </div>
</form>

座位-modal.component.ts

import { Component, OnInit } from '@angular/core';
import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import { SocketService} from '../../_services/socket.service';
import {AlertService} from '../../_services/alert.service';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-seat-modal',
  templateUrl: './seat-modal.component.html',
  styleUrls: ['./seat-modal.component.less']
})
export class SeatModalComponent implements OnInit {

  formRequestSeat: FormGroup;
  socket;
  seat;
  showModal: boolean;
  screenName = '';

  constructor(private socketService: SocketService,
              private modalService: NgbModal,
              private formBuilder: FormBuilder,
              private alertService: AlertService,
              private router: Router,
              private activatedRoute: ActivatedRoute) { }

  ngOnInit(){
    this.formRequestSeat = this.formBuilder.group({
      seatRequest: ['', Validators.required]
    });
  }

  hide(){
    this.modalService.dismissAll();
  }

  join(seat){
    const self = this;

    const tableToken = localStorage.getItem('tableToken');
    const userToken = localStorage.getItem('userToken');

    self.socket = self.socketService.init(userToken, tableToken);

    self.socket.emit('join', {tableToken, seat}, (gameObject) => {
        if (gameObject.error === null) {
          self.alertService.success(gameObject.message, true);
        }
        else {
          self.alertService.error(gameObject.message, true);
        }
        self.hide();
      });
  }
}

之前我使用模式弹出窗口而不是弹出窗口。我可以将按钮的 idx 获取到弹出模式,但我不知道如何将 idx 传递给我的 popover.Please 帮助

<div class="container">
      <div [formGroup]="seatsFormGroup">
        <div formArrayName="seatsArray" *ngFor="let seat of seats;let idx = index" id="seat-{{idx}}" class="col-sm-4 ">
          <label  [ngbPopover]="popOver" popoverTitle="Pop title" class="table-player-seat-button" (click)="sit(idx)">{{seat.displayName? seat.displayName: "Sit "+ idx}}</label>

         <ng-template #popOver>
            <app-seat-modal [btnIdx]="idx"></app-seat-modal>
          </ng-template>


        </div>
      </div>
    </div>

在 app-seat-modal 组件中

import { Component, OnInit } from '@angular/core';
import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import { SocketService} from '../../_services/socket.service';
import {AlertService} from '../../_services/alert.service';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-seat-modal',
  templateUrl: './seat-modal.component.html',
  styleUrls: ['./seat-modal.component.less']
})
export class SeatModalComponent implements OnInit {
  @Input() btnIdx : number;
  formRequestSeat: FormGroup;
  socket;
  seat;
  showModal: boolean;
  screenName = '';

  constructor(private socketService: SocketService,
              private modalService: NgbModal,
              private formBuilder: FormBuilder,
              private alertService: AlertService,
              private router: Router,
              private activatedRoute: ActivatedRoute) { }

  ngOnInit(){
    this.formRequestSeat = this.formBuilder.group({
      seatRequest: ['', Validators.required]
    });
  }

  hide(){
    this.modalService.dismissAll();
  }

  join(seat){
    const self = this;

    const tableToken = localStorage.getItem('tableToken');
    const userToken = localStorage.getItem('userToken');

    self.socket = self.socketService.init(userToken, tableToken);

    self.socket.emit('join', {tableToken, seat}, (gameObject) => {
        if (gameObject.error === null) {
          self.alertService.success(gameObject.message, true);
        }
        else {
          self.alertService.error(gameObject.message, true);
        }
        self.hide();
      });
  }
}