如果满足条件,Angular(4或2)将应用CSS

时间:2022-04-24 18:36:12

I've found some code examples that explain how I can apply a class item if conditions are met.

我找到了一些代码示例,解释了如果满足条件我如何应用类项。

In my .ts file I have the following:

在我的.ts文件中,我有以下内容:

private readNews : boolean = false;

[..]

  ngOnInit() {
    localStorage.setItem('readNews', 'Read');

    if (localStorage.getItem('readNews') != null || '') {
      this.readNews = true;
    }
  }

In my HTML I have the following inline CSS:

在我的HTML中,我有以下内联CSS:

<i class="fal fa-cogs fa-5x"></i>

However, what I want it to be is the following:

但是,我想要的是以下内容:

If this.readNews === true

如果this.readNews === true

<i class="fal fa-cogs fa-5x blink"></i>

So it needs to add 'blink' in the CSS when the news is read (which is saved in localStorage).

因此,当读取新闻时(需要在localStorage中保存),需要在CSS中添加“闪烁”。

4 个解决方案

#1


4  

try like this :

试试这样:

<i [ngClass]="readNews ? 'fal fa-cogs fa-5x blink': 'fal fa-cogs fa-5x'"></i>

#2


4  

Use ngClass

<i [ngClass]="(readNews == 'Read')? 'fal fa-cogs fa-5x blink':'fal fa-cogs fa-5x'" ></i>

or you can call custom functions

或者你可以调用自定义功能

[ngClass]="getClass()"

getClass(flag:string) {
  let cssClasses;
  if(localStorage.getItem('readNews') == 'Read') {  
     cssClasses = {
       'fal': true,
       'fa-cogs': true,
       'fa-5x': true,
       'blink': true
     }  
  } else {  
     cssClasses = {
       'fal': true,
       'fa-cogs': true,
       'fa-5x': true,
       'blink': false
     }  
  }
  return cssClasses;
}

#3


0  

<i [class.blink]="readNews"></i>

Based on cheat sheet on https://angular.io/guide/cheatsheet

基于https://angular.io/guide/cheatsheet上的备忘单

#4


0  

In HTML:

<i *ngIf="readNews" class="fal fa-cogs fa-5x"></i>
<i *ngIf="!readNews" class="fal fa-cogs fa-5x blink"></i>

And in typescript i would refactor as this to improve readability:

在打字稿中,我会重构,以提高可读性:

ngOnInit() {
localStorage.setItem('readNews', 'Read');
this.readNews = (localStorage.getItem('readNews') != null || '');

}

#1


4  

try like this :

试试这样:

<i [ngClass]="readNews ? 'fal fa-cogs fa-5x blink': 'fal fa-cogs fa-5x'"></i>

#2


4  

Use ngClass

<i [ngClass]="(readNews == 'Read')? 'fal fa-cogs fa-5x blink':'fal fa-cogs fa-5x'" ></i>

or you can call custom functions

或者你可以调用自定义功能

[ngClass]="getClass()"

getClass(flag:string) {
  let cssClasses;
  if(localStorage.getItem('readNews') == 'Read') {  
     cssClasses = {
       'fal': true,
       'fa-cogs': true,
       'fa-5x': true,
       'blink': true
     }  
  } else {  
     cssClasses = {
       'fal': true,
       'fa-cogs': true,
       'fa-5x': true,
       'blink': false
     }  
  }
  return cssClasses;
}

#3


0  

<i [class.blink]="readNews"></i>

Based on cheat sheet on https://angular.io/guide/cheatsheet

基于https://angular.io/guide/cheatsheet上的备忘单

#4


0  

In HTML:

<i *ngIf="readNews" class="fal fa-cogs fa-5x"></i>
<i *ngIf="!readNews" class="fal fa-cogs fa-5x blink"></i>

And in typescript i would refactor as this to improve readability:

在打字稿中,我会重构,以提高可读性:

ngOnInit() {
localStorage.setItem('readNews', 'Read');
this.readNews = (localStorage.getItem('readNews') != null || '');

}