Nativescript Angular 2-单击将类添加到标签

Nativescript Angular 2-单击将类添加到标签,angular,typescript,nativescript,Angular,Typescript,Nativescript,我想在我的nativescript angular 2应用程序中实现一个like按钮(如instagram、twitter等)。单击标签应将“活动”类添加到标签中。再次单击后,它应该删除“活动”类 home.component.html <label text="{{ post.likes }}" class="" (tap)="like()"> 如果喜欢或不喜欢,可以使用存储的变量。因此,它将添加该类或从元素中删除该类 例如: @组件({ 选择器:“应用程序元素”, 模板:“你好

我想在我的nativescript angular 2应用程序中实现一个like按钮(如instagram、twitter等)。单击标签应将“活动”类添加到标签中。再次单击后,它应该删除“活动”类

home.component.html

<label text="{{ post.likes }}" class="" (tap)="like()">

如果喜欢或不喜欢,可以使用存储的变量。因此,它将添加该类或从元素中删除该类 例如:

@组件({
选择器:“应用程序元素”,
模板:“你好帖子”,
风格:[`
.喜欢{
颜色:红色
}
`]
})
导出类AppComponent{
喜欢=错误;
切换(post):无效{
post.liked=!post.liked;
}
}
注意:由于对象引用而起作用。 示例:

您需要在此处使用


确保您使用大写字母
L
标签
可以只使用
(点击)=“liked=!liked”
,对于如此简单的事情不需要方法。还要确保在组件中声明变量,
public-liked:boolean
,或作为
输入()
,如果它用作子组件。是的,可以这样做,但我在类似函数中添加了,假设OP可能希望在标签单击上执行更多操作。
<label text="{{ post.likes }}" class="active" (tap)="like()>
like() {
    if (/* post contains "active" class */) {
        // remove "active" class to label
    } else {
        // add "active" class to label
    }
}
<Label text="{{ post.likes }}" [ngClass]="liked ? 'active' : ''" (tap)="like()></Label>
like() {
//toggle this.liked here.
}