137

How to declare a function inside a component (typescript) and call it on a click event in Angular 2? Following is the code for the same functionality in Angular 1 for which I require Angular 2 code:

<button ng-click="myFunc()"></button>

//controller

app.controller('myCtrl', ['$scope', function($cope) {
    $scope.myFunc= {
        console.log("function called");
    };
}]);
6
  • 7
    Why did you tag angular2 if this is an Angular 1 app? Commented Oct 24, 2016 at 6:51
  • Scant description, structurally wrong sentences, ugly code. all of the result in wrong understanding. people this this is an Angular1 question! Commented Oct 24, 2016 at 7:12
  • 3
    Whatever I have written in code I want do same in angular2. Commented Oct 24, 2016 at 7:29
  • Old post, but I recommend looking at the "tour of Heros" from the angular 2 documentation. Commented Apr 6, 2017 at 14:37
  • Is it Angular 1 or Angular 2? It should've been specified Commented Feb 18, 2018 at 15:10

5 Answers 5

159

Component code:

import { Component } from "@angular/core";

@Component({
  templateUrl:"home.html"
})
export class HomePage {

  public items: Array<string>;

  constructor() {
    this.items = ["item1", "item2", "item3"]
  }

  public open(event, item) {
    alert('Open ' + item);
  }

}

View:

<ion-header>
  <ion-navbar primary>
    <ion-title>
      <span>My App</span>
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <ion-list>
    <ion-item *ngFor="let item of items" (click)="open($event, item)">
      {{ item }}
    </ion-item>
  </ion-list>
</ion-content>

As you can see in the code, I'm declaring the click handler like this (click)="open($event, item)" and sending both the event and the item (declared in the *ngFor) to the open() method (declared in the component code).

If you just want to show the item and you don't need to get info from the event, you can just do (click)="open(item)" and modify the open method like this public open(item) { ... }

Sign up to request clarification or add additional context in comments.

2 Comments

how about disappear this menu after logoff? my databootstrapperpage using menu like this and every other push page has their own but after logoff and goto login page this menu unexpected appeared so after click disappeared but icant handle this.
@sabertabatabaeeyazdi could you please create a Stackblitz demo with your issue so I can take a look at it?
79

Exact transfer to Angular2+ is as below:

<button (click)="myFunc()"></button>

also in your component file:

import { Component, OnInit } from "@angular/core";

@Component({
  templateUrl:"button.html" //this is the component which has the above button html
})

export class App implements OnInit{
  constructor(){}

  ngOnInit(){

  }

  myFunc(){
    console.log("function called");
  }
}

Comments

10

https://angular.io/guide/user-input - there's a simple example .

1 Comment

Referring to original documentations is a better approach since the is a great chance of learning more.
7

This worked for me: :)

<button (click)="updatePendingApprovals(''+pendingApproval.personId, ''+pendingApproval.personId)">Approve</button>
updatePendingApprovals(planId: string, participantId: string) : void {
  alert('PlanId:' + planId + '    ParticipantId:' + participantId);
}

Comments

4

The line in your controller code, which reads $scope.myFunc={ should be $scope.myFunc = function() { the function() part is important to indicate, it is a function!

The updated controller code would be

app.controller('myCtrl',['$scope',function($cope){
    $scope.myFunc = function() {
    console.log("function called");
  };
}]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.