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>
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>
Share
Improve this question
asked Mar 3, 2017 at 11:33
Andrew HowardAndrew Howard
3,0726 gold badges40 silver badges71 bronze badges
8
|
Show 3 more comments
3 Answers
Reset to default 14This should work:
<a (click)="page > 1 ? setPage(page - 1) : null">Previous</a></li>
similar example: http://plnkr.co/edit/ojO0GwQktneBuzKqKTwz?p=preview
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>
You can do this
<a (click)="page > 1 ? setPage(page - 1) : null">Previous</a></li>
onLinkClick(page)
to it and let the method handle itif(page > 1) ...do something
– cyr_x Commented Mar 3, 2017 at 11:36(click)="page > 1 ? setPage(page - 1) : void"
, assuming thatsetPage
has a void return. – Kallum Tanton Commented Mar 3, 2017 at 11:36setPage
method is coupled to the anchor - it's likely that other things will call this method and thepage > 1
logic will not be applicable. – Kallum Tanton Commented Mar 3, 2017 at 11:37