John O
John O

Reputation: 5473

How do I use the browser's confirm() along with an Angular template statement?

I'm trying to do this:

<button (click)="confirm('some message') ? someFunction() : anotherFunction()">Text</button>

This of course fails, because Angular looks for the confirm() function within its own code. It fails compilation with an error statement like so:

ERROR in src\client\pages\x.component.html(72,114): : Property 'confirm' does not exist on type 'OurCustomComponent'.

Is there a way to make this work? I do realize (having just read the documentation now), that some javascript syntax is disallowed, but it's unclear if there's a way to accomplish this or not.

Upvotes: 1

Views: 35

Answers (1)

satanTime
satanTime

Reputation: 13584

you need a wrapper function in the component.

confirm(): void {
  confirm('some message') ? this.someFunction() : this.anotherFunction();
}
<button (click)="confirm()">Text</button>

Upvotes: 1

Related Questions