【问题标题】:Toggle Angular Component CSS Class based on Array Values基于数组值切换 Angular 组件 CSS 类
【发布时间】:2019-07-14 15:17:17
【问题描述】:

我是 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'

【问题讨论】:

  • 那么你想显示喜欢计数吗?
  • @PrashantPimpale 不,我不想显示计数,我只想添加一个类将颜色更改为红色
  • 使用ngClass,如here所示
  • @Musa 好的,然后检查 ngClass。试着让我们知道工作与否
  • 感谢 @PrashantPimpale 我在我的 html 中实现了 [class.feed-icon-red]="isLiked(post)" 并且它有效

标签: javascript angular typescript


【解决方案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

【讨论】:

    【解决方案2】:

    您判断帖子是否被点赞的逻辑似乎存在问题。

    来,试试看:

    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 供您参考。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多