Mike Johnson
Mike Johnson

Reputation: 29

Unable to create window alert in Angular 7 (click) function

I am unable to create a Chrome alert dialog when clicking on a button.

<button mat-raised-button matStepperNext color="primary" [disabled]="formGroup.invalid" (click)="window.alert('Thanks for your submission!')>Submit</button>

What am I doing wrong?

Upvotes: 1

Views: 6001

Answers (2)

scarmoose
scarmoose

Reputation: 83

In your component.ts file you can say

window = window;

And then use window in your component.html as you would anywhere else

<button (click)="window.alert('Hello world')">
 This is a button
</button>

Upvotes: 1

Lewis Gardner
Lewis Gardner

Reputation: 86

You can not access "window" from the component.html file.

Instead, try creating an alert function in your component.ts file like so:

alert() {
  window.alert('test');
}

Then run the alert() function:

<button mat-raised-button matStepperNext color="primary" [disabled]="formGroup.invalid" (click)="alert()">Submit</button>

Upvotes: 7

Related Questions