admin管理员组文章数量:1208153
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>
本文标签: javascriptAngular 2 assign a click function using a ternary operatorStack Overflow
版权声明:本文标题:javascript - Angular 2 assign a click function using a ternary operator - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738682040a2106609.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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