gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:07:17 27 4
gpt4 key购买 nike

我想在我的 nativescript angular 2 应用程序中实现一个点赞按钮(比如 instagram、twitter 等)。单击标签应将“事件”类添加到标签中。再次点击后,它应该会移除“active”类。

home.component.html

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

home.component.html(点击标签后)

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

home.component.ts

like() {
if (/* post contains "active" class */) {
// remove "active" class to label
} else {
// add "active" class to label
}
}

最佳答案

您需要使用 ngClass在这里。

<Label text="{{ post.likes }}" [ngClass]="liked ? 'active' : ''" (tap)="like()></Label>

在你的 home.component.ts 中

like() {
//toggle this.liked here.
}

关于angular - Nativescript Angular 2 - 单击时将类添加到标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55244558/

27 4 0