Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 assign a click function using a ternary operator

I would like to apply a click function:

setPage(page - 1)

But only if this condition matches:

page > 1

I thought I could do it like this but it didn't work, any ideas?

<a (click)="{'setPage(page - 1)' : page > 1}">Previous</a></li>
like image 566
Andrew Junior Howard Avatar asked Mar 03 '17 11:03

Andrew Junior Howard


2 Answers

This should work:

<a (click)="page > 1 ? setPage(page - 1) : null">Previous</a></li>

similar example: http://plnkr.co/edit/ojO0GwQktneBuzKqKTwz?p=preview

like image 57
eko Avatar answered Sep 20 '22 04:09

eko


As mentioned in the comments:

Create a new method in your component called for example onAnchorClick and let it handle the logic.

public onAnchorClick(page: number) {
   if(page > 1) {
     this.setPage(page - 1);
     // some other stuff to do
   }
}

and bind it to your Anchor

<a (click)="onAnchorClick(page)">Previous</a>
like image 27
cyr_x Avatar answered Sep 21 '22 04:09

cyr_x