Handling events


#1

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


#2

@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');

}

#3

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


#4

@yep but u tagged ionic not ionic2


#5

yes you are right :relaxed:


#6

i think it’s like this in ionic2

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

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

#7

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:


#8
import {Page} from 'ionic/ionic';

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

}

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

}

#9

Ok thank you very much :grin: