Handling events

Hello I don’t know how to add a click event to a component. Can anyone help me
thank you :relieved:

@ameni

ng-click directive is used in angularjs to handle click event
try like this in your template

<div ng-click = "click_me()">

</div> 

and In Controller handle this click event

$scope.click_me = function(){

alert('Hello');

}

thank you but I am using ionic 2
events are handled in another way I suppose

@yep but u tagged ionic not ionic2

1 Like

yes you are right :relaxed:

i think it’s like this in ionic2

<button (click)="clicked()">Click</button>

@Component(...)
class MyComponent {
  clicked(event) {
  }
}
1 Like

hello thank you for helping me
I didn’t understand too much
I guess pages in ionic2 are considered as components.

Here is my HTML code containing only one button

<ion-content padding class="page1">
<button secondary (click)="clicked()">Secondary</button>
</ion-content>

Here is my script

import {Page} from ‘ionic/ionic’;

@Page({
templateUrl: ‘build/pages/page1/page1.html’
})
export class Page1 {
constructor() {

}

}

Where can I add the function clicked()
Thank you :slightly_smiling:

import {Page} from 'ionic/ionic';

@Page({
templateUrl: 'build/pages/page1/page1.html'
})
export class Page1 {
constructor() {

}

clicked() { 
      console.log('CLICKED!!'); 
 }

}
1 Like

Ok thank you very much :grin: