Anjali
Anjali

Reputation: 2698

passing the value of the button to the angular 2

I am very new to angular and trying to pass the value of the Cancel button to angular. I have the following code in Cancel button

 <button class="appBtn"  mat-button [mat-dialog-close] ="data">
          Cancel
 </button>

I want to capture the button value Cancel on the angular so that can make some variables null. Is it possible to do this or is thier a different way?

Upvotes: 0

Views: 90

Answers (2)

julianobrasil
julianobrasil

Reputation: 9367

Why don't you just pass it with the data?

<button class="appBtn" mat-button [mat-dialog-close]="{btn: 'cancel', data: data}">
  Cancel
</button>

Supposing this is the code that fired the dialog in the first place:

showDialog() {
  const dialogRef = this._matDialog.open(MyDialogComponent);

  dialogRef.afterClosed().subscribe((value) => {
    console.log({'pressed button': value.btn, data: value.data});
  });
}

Upvotes: 1

Nikhil
Nikhil

Reputation: 6641

Add a click event handler on your button and perform operations in its handling function.

<button class="appBtn" mat-button [mat-dialog-close] ="data" (click)="onClick($event)">
  Cancel
</button>

Component:

onClick(event) {
 // Capture button name.
 var buttonName = event.target.name;

 // Make variables null.
}

Upvotes: 0

Related Questions