基于数组值切换角度组件CSS类

时间:2019-07-14 06:42:45

标签: javascript angular typescript

我是Angular的新手,正在Angular 8中使用我的第一个应用程序,我想根据对象数组值切换组件CSS类。

对象是带有喜欢数组的帖子(test-data / posts.ts)

export const posts = [{
    id: 1,
    author: {},
    type: 'image',
    media: '',
    repost: 0,
    comments: [],
    likes: [ 1, 2, 3, 4]
}];

所以我的组件打字稿是这个,要在帖子上添加和删除一个赞,就可以了

import { Component } from '@angular/core';

import { user } from '../test-data/user';
import { posts } from '../test-data/posts';

@Component({
    selector: 'app-feed',
    templateUrl: './feed.component.html',
    styleUrls: ['./feed.component.css'],
})
export class FeedComponent {
    posts = posts;
    user = user;

    liked = false;

    likePost(post) {
        // If post is liked, remove like
        if (this.isLiked(post)) {
            for (let i = 0; i < post.likes.length; i++) {
                if (post.likes[i] === user.id) { post.likes.splice(i, 1); }
            }
        // If not liked add a new like
        } else {
            post.likes.push(user.id);
        }
        // DEBUG: Log post data
        console.log(post);
    }

    isLiked(post) {
        return post.likes.includes(user.id);
    }
}

我可以检索对象,使用组件添加/删除喜欢的对象,但是如何检查数组以确定帖子应显示为喜欢还是不同?

我当前的html模板是这个,如果用户ID(user.id)出现在数组(post.likes)之类的帖子中,我希望拥有一个额外的类“ feed-icon-red”

<div *ngIf="posts.length">
    <div *ngFor="let post of posts">
        <mat-list>
            <!-- Post Media -->
            <img class="feed-image" [src]="post.media" (dblclick)="likePost(post)"/>

            <!-- Interaction Button Row -->
            <mat-list-item>
                <span>
                    <mat-icon mat-list-icon class="ti-heart" [class.feed-icon-red]="" (dblclick)="likePost(post)"></mat-icon>
                </span>
            </mat-list-item>
        </mat-list>
    </div>
</div>

编辑:我想检查数组中的特定值以设置类。 例如if(post.likes.includes(1))将图标类别设置为“ feed-icon-red”

2 个答案:

答案 0 :(得分:1)

您可以使用ngClass:

[ngClass]="isLiked(post) ? 'feed-icon-red' : ''"

甚至这也可行:

[class.feed-icon-red]="isLiked(post)"

HTML代码:

<div *ngIf="posts.length">
    <div *ngFor="let post of posts">
        <mat-list>
            <!-- Post Media -->
            <img  width=300 class="feed-image" [src]="post.media" (dblclick)="likePost(post)"/>
            <!-- Interaction Button Row -->
            <mat-list-item>
              {{isLiked(post)}}
                <span [ngClass]="isLiked(post)? 'feed-icon-red': ''"> Test
                    <mat-icon mat-list-icon class="ti-heart"  (dblclick)="likePost(post)"></mat-icon>
                </span>
            </mat-list-item>
        </mat-list>
    </div>
</div>

Working-Demo

答案 1 :(得分:0)

确定帖子是否被喜欢的逻辑似乎存在问题。

在这里,尝试一下:

import { Component } from '@angular/core';
import { user } from './test-data/user';
import { posts } from './test-data/posts';

/**
 * @title Basic list
 */
@Component({
  selector: 'list-overview-example',
  templateUrl: 'list-overview-example.html',
  styleUrls: ['list-overview-example.css'],
})
export class ListOverviewExample {
  posts = posts;
  user = user;

  liked = false;

  likePost(post) {
    // If post is liked, remove like
    if (this.isLiked(post)) {
      post.likes.splice(post.likes.indexOf(this.user.id), 1);
      // If not liked add a new like
    } else {
      post.likes.push(this.user.id);
    }
    // DEBUG: Log post data
    console.log(post);
  }

  isLiked(post) {
    return post.likes.includes(this.user.id);
  }
}

在您的模板中:

<div *ngIf="posts.length">
    <ul>
    <li *ngFor="let post of posts">
        <!-- Post Media -->
        <img class="feed-image" [src]="post.media" (dblclick)="likePost(post)"/>
    <!-- Interaction Button Row -->
    <span [class.feed-icon-red]="post.likes.includes(user.id)" (dblclick)="likePost(post)">❤</span>
    </li>
  </ul>
</div>

  

这是您推荐的Working Sample StackBlitz