Eswar
Eswar

Reputation: 107

How to perform javascript onclick operation in Angular 5?

contend included in app.component.html file

<button onclick="myfunction()">Click me </button>
<script src="one.js"></script>

one.js file

function myfunction() {
  alert();
}

Upvotes: 1

Views: 5133

Answers (2)

Sajith
Sajith

Reputation: 173

In app.component.html add click event:

<button (click)="myfunction()">Click me </button>

In app.component.ts add its corresponding function:

export class AppComponent {
  title = 'app';
 myfunction(){
      alert();
  }
}

Its working for me.

Upvotes: 3

P.S.
P.S.

Reputation: 16384

In Angular the regular onclick should be (click) or on-click, like this:

<button (click)="myfunction()">Click me </button>

Or this:

<button on-click="myfunction()">Click me </button>

For more info you can read about the event binding in Angular.

Upvotes: 1

Related Questions