4

This is a part of a bigger project that I have tried to simplify through this example. I use PrimeNg Babylon theme. I have parent component which has a for loop that is generating a button and a child component with dialog Box inside it for each iteration. On click of this button is activated PrimeNg Dialog. But as it is a loop of buttons and dialogs, on click of any of these button every dialog box is opened. (3 in sum). The problem is that i don't have idea how to target on click of the button only it's appertaining dialog box from parent to child component, and not of all buttons. I am new with Angular. Can somebody help? Code of the app.component.html parent component is:

  <div *ngFor="let item of items; let i = index">
    <button type="button" (click)="showDialog(i)" pButton icon="pi pi-info-circle" label="Show"></button>
    <app-child (notify)="parentListener($event)"></app-child>
  </div>
</div>

Code of the app.component.ts is:


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  display = false;
  items = [
    {1: 1}, {2: 2}, {3: 3}
  ];

  showDialog() {
    this.display = true;
  }

  parentListener(data) {
    this.display = data;
  }
}

Code of child.component.html is:

<p-dialog header="Godfather I" [(visible)]="display" [modal]="true" [responsive]="true" [style]="{width: '350px', minWidth: '200px'}" [minY]="70"
          [maximizable]="true" [baseZIndex]="10000">
  <p>The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding.
    His beloved son Michael has just come home from the war, but does not intend to become part of his father's business.
    Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family,
    kind and benevolent to those who give respect,
    but given to ruthless violence whenever anything stands against the good of the family.</p>
  <p-footer>
    <button type="button" pButton icon="pi pi-check" (click)="display=false" label="Yes"></button>
    <button type="button" pButton icon="pi pi-close" (click)="returningDataToParent()" label="No" class="ui-button-secondary"></button>
  </p-footer>
</p-dialog>

and the code of the child.component.ts is:

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
  @Input ()
  display: boolean;
@Output ()
notify: EventEmitter<boolean> = new EventEmitter<boolean>();

  constructor() { }

  ngOnInit() {

  }

  returningDataToParent() {
    this.notify.emit(false);
  }
}
Ivana
  • 842
  • 2
  • 15
  • 33
  • parentListener is the listener function, when i click on cancel button in dialog box of app-child to close it. I will delete i argument as i don' t use it on showDialog – Ivana Nov 21 '19 at 05:45

0 Answers0