Angular: *ngclass的有条件类

发布于 2025-01-31 04:50:05 字数 458 浏览 4 评论 0 原文

我的角度代码怎么了?我收到以下错误:

无法在browserdomAdapter.removeclass

上读取“删除”不确定的属性。
<ol>
  <li *ngClass="{active: step==='step1'}" (click)="step='step1'">Step1</li>
  <li *ngClass="{active: step==='step2'}" (click)="step='step2'">Step2</li>
  <li *ngClass="{active: step==='step3'}" (click)="step='step3'">Step3</li>
</ol>

What is wrong with my Angular code? I am getting the following error:

Cannot read property 'remove' of undefined at BrowserDomAdapter.removeClass

<ol>
  <li *ngClass="{active: step==='step1'}" (click)="step='step1'">Step1</li>
  <li *ngClass="{active: step==='step2'}" (click)="step='step2'">Step2</li>
  <li *ngClass="{active: step==='step3'}" (click)="step='step3'">Step3</li>
</ol>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(25

白昼 2025-02-07 04:50:05

Angular版本2+提供了几种有条件添加类别的方法:

一型

[class.my_class] = "step === 'step1'"

type 2

[ngClass]="{'my_class': step === 'step1'}"

和多个选项:

[ngClass]="{'my_class': step === 'step1', 'my_class2' : step === 'step2' }"

type三

[ngClass]="{1 : 'my_class1', 2 : 'my_class2', 3 : 'my_class4'}[step]"

type四

[ngClass]="step == 'step1' ? 'my_class1' : 'my_class2'"

您可以在

Angular version 2+ provides several ways to add classes conditionally:

Type one

[class.my_class] = "step === 'step1'"

Type two

[ngClass]="{'my_class': step === 'step1'}"

and multiple options:

[ngClass]="{'my_class': step === 'step1', 'my_class2' : step === 'step2' }"

Type three

[ngClass]="{1 : 'my_class1', 2 : 'my_class2', 3 : 'my_class4'}[step]"

Type four

[ngClass]="step == 'step1' ? 'my_class1' : 'my_class2'"

You can find these examples on the the documentation page.

孤芳又自赏 2025-02-07 04:50:05

[ngclass] = ... 而不是*ngclass

*仅适用于结构指令的速记语法,例如,您可以在其中

<div *ngFor="let item of items">{{item}}</div>

而不是较长的等价版本,

<template ngFor let-item [ngForOf]="items">
  <div>{{item}}</div>
</template>

另请参见 ngclass

 &lt; some-element [ngclass] =“'第一秒'&gt; ...&lt; ...
&lt; some-element [ngclass] =“ ['first','second']”&gt; ...&lt;/some-element&gt;
&lt; some-element [ngclass] =“ {'first':true,'second':true,'thixt':false}”&gt; ...&lt;/some-element&gt;
&lt; some-element [ngclass] =“ stringexp | arrayexp | objexp”&gt; ...&lt;/some-element&gt;
&lt; some-element [ngclass] =“ {'class1 class2 class3':true}”&gt; ...&lt;/some-element&gt;
 

另请参见 syntax syntax

 &lt;!
&lt; div [class.pecial] =“ isspecial”&gt;类绑定是特殊的&lt;/div&gt;

!
&lt; div class =“特殊”
     [class.special] =“!isspecial”&gt;这个不是那么特别&lt;/div&gt;
 
 &lt;!
&lt; div class =“坏卷曲特殊”
     [class] =“ badcurly”&gt; bad curly&lt;/div&gt;
 

[ngClass]=... instead of *ngClass.

* is only for the shorthand syntax for structural directives where you can for example use

<div *ngFor="let item of items">{{item}}</div>

instead of the longer equivalent version

<template ngFor let-item [ngForOf]="items">
  <div>{{item}}</div>
</template>

See also NgClass.

<some-element [ngClass]="'first second'">...</some-element>
<some-element [ngClass]="['first', 'second']">...</some-element>
<some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element>
<some-element [ngClass]="stringExp|arrayExp|objExp">...</some-element>
<some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element>

See also Template syntax

<!-- Toggle the "special" class on/off with a property -->
<div [class.special]="isSpecial">The class binding is special</div>

<!-- Binding to `class.special` trumps the class attribute -->
<div class="special"
     [class.special]="!isSpecial">This one is not so special</div>
<!-- Reset/override all class names with a binding  -->
<div class="bad curly special"
     [class]="badCurly">Bad curly</div>
惟欲睡 2025-02-07 04:50:05

另一个解决方案是使用 [class.active]

例子:

<ol class="breadcrumb">
    <li [class.active]="step=='step1'" (click)="step='step1'">Step1</li>
</ol>

Another solution would be using [class.active].

Example:

<ol class="breadcrumb">
    <li [class.active]="step=='step1'" (click)="step='step1'">Step1</li>
</ol>
七禾 2025-02-07 04:50:05

那是正常的结构。对于 ngclass ,它是:

[ngClass]="{'classname' : condition}"

因此,在您的情况下,只需这样使用...

<ol class="breadcrumb">
  <li [ngClass]="{'active': step==='step1'}" (click)="step='step1'">Step1</li>
  <li [ngClass]="{'active': step==='step2'}" (click)="step='step2'">Step2</li>
  <li [ngClass]="{'active': step==='step3'}" (click)="step='step3'">Step3</li>
</ol>

That's the normal structure. For ngClass, it is:

[ngClass]="{'classname' : condition}"

So in your case, just use it like this...

<ol class="breadcrumb">
  <li [ngClass]="{'active': step==='step1'}" (click)="step='step1'">Step1</li>
  <li [ngClass]="{'active': step==='step2'}" (click)="step='step2'">Step2</li>
  <li [ngClass]="{'active': step==='step3'}" (click)="step='step3'">Step3</li>
</ol>
淡墨 2025-02-07 04:50:05

在以下示例中,您可以使用“如果其他”:

<p class="{{condition ? 'checkedClass' : 'uncheckedClass'}}">
<p [ngClass]="condition ? 'checkedClass' : 'uncheckedClass'">
<p [ngClass]="[condition ? 'checkedClass' : 'uncheckedClass']">

With the following examples, you can use 'IF ELSE':

<p class="{{condition ? 'checkedClass' : 'uncheckedClass'}}">
<p [ngClass]="condition ? 'checkedClass' : 'uncheckedClass'">
<p [ngClass]="[condition ? 'checkedClass' : 'uncheckedClass']">
回忆那么伤 2025-02-07 04:50:05

您可以使用ngclass进行有条件地应用班级名称,而不是在角度上应用

[ngClass]="'someClass'">

条件

[ngClass]="{'someClass': property1.isValid}">

多种条件

 [ngClass]="{'someClass': property1.isValid && property2.isValid}">

方法表达式

[ngClass]="getSomeClass()"

此方法将内部您的组件

 getSomeClass(){
        const isValid=this.property1 && this.property2;
        return {someClass1:isValid , someClass2:isValid};
    }

You can use ngClass to apply the class name both conditionally and not in Angular

For Example

[ngClass]="'someClass'">

Conditional

[ngClass]="{'someClass': property1.isValid}">

Multiple Condition

 [ngClass]="{'someClass': property1.isValid && property2.isValid}">

Method expression

[ngClass]="getSomeClass()"

This method will inside of your component

 getSomeClass(){
        const isValid=this.property1 && this.property2;
        return {someClass1:isValid , someClass2:isValid};
    }
筱果果 2025-02-07 04:50:05

Angular提供了有条件添加类别的多种方法:

第一种方式

活动是您的类名称

[class.active]="step === 'step1'"

第二种方法

active是您的类名称

[ngClass]="{'active': step=='step1'}"

第三路

通过使用三元运算符class1和class2是您的班级名称

[ngClass]="(step=='step1')?'class1':'class2'"

Angular provides multiple ways to add classes conditionally:

First way

active is your class name

[class.active]="step === 'step1'"

Second way

active is your class name

[ngClass]="{'active': step=='step1'}"

Third way

by using ternary operator class1 and class2 is your class name

[ngClass]="(step=='step1')?'class1':'class2'"
回心转意 2025-02-07 04:50:05

您应该使用某些东西( [ngclass] 而不是*ngclass )这样:

<ol class="breadcrumb">
  <li [ngClass]="{active: step==='step1'}" (click)="step='step1; '">Step1</li>
  (...)
</ol>

You should use something ([ngClass] instead of *ngClass) like that:

<ol class="breadcrumb">
  <li [ngClass]="{active: step==='step1'}" (click)="step='step1; '">Step1</li>
  (...)
</ol>
于我来说 2025-02-07 04:50:05

在Angular 7.x中,

CSS类的更新如下,具体取决于表达式评估的类型:

  • String-添加了字符串中列出的CSS类(Space Delimited)

  • array-添加了为数组元素的CSS类

  • 对象 - 键是CSS类,当值评估为真实值时,在值中给出的表达式时会添加。否则,将其删除。

<some-element [ngClass]="'first second'">...</some-element>

<some-element [ngClass]="['first', 'second']">...</some-element>

<some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element>

<some-element [ngClass]="stringExp|arrayExp|objExp">...</some-element>

<some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element>

In Angular 7.X

The CSS classes are updated as follows, depending on the type of the expression evaluation:

  • string - the CSS classes listed in the string (space delimited) are added

  • Array - the CSS classes declared as Array elements are added

  • Object - keys are CSS classes that get added when the expression given in the value evaluates to a truthy value. Otherwise, they are removed.

<some-element [ngClass]="'first second'">...</some-element>

<some-element [ngClass]="['first', 'second']">...</some-element>

<some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element>

<some-element [ngClass]="stringExp|arrayExp|objExp">...</some-element>

<some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element>
酒儿 2025-02-07 04:50:05

返回的值:

,您可以分配一个函数

<div [ngClass]="setClasses()">...</div>

此外

// Set Dynamic Classes
  setClasses() {
    let classes = {
      constantClass: true,
      'conditional-class': this.item.id === 1
    }

    return classes;
  }

Additionally, you can assign a value returned by a function:

In HTML

<div [ngClass]="setClasses()">...</div>

In component.ts

// Set Dynamic Classes
  setClasses() {
    let classes = {
      constantClass: true,
      'conditional-class': this.item.id === 1
    }

    return classes;
  }
思念满溢 2025-02-07 04:50:05

要扩展 mostafamashayekhi的答案您也可以选择二&gt; ','的选项*ngif

[ngClass]="{'my-class': step=='step1', 'my-class2':step=='step2' }"

也可以在某些情况下与 *ngfor结合的某些情况中使用

class="mats p" *ngIf="mat=='painted'"

To extend MostafaMashayekhi's answer for option two>, you can also chain multiple options with a ','

[ngClass]="{'my-class': step=='step1', 'my-class2':step=='step2' }"

Also *ngIf can be used in some of these situations usually combined with a *ngFor

class="mats p" *ngIf="mat=='painted'"
酒中人 2025-02-07 04:50:05

您可以使用[ngclass]或[class.classname],两个都可以工作。
[class.my-class] =“ step ==='step1'”

&nbsp;&nbsp;或

[ngclass] =“ {'my-class':step =='step1'}”

两个都可以工作!

You can use [ngClass] or [class.classname], both will work the same.
[class.my-class]="step==='step1'"

   OR

[ngClass]="{'my-class': step=='step1'}"

Both will work the same!

习惯那些不曾习惯的习惯 2025-02-07 04:50:05

您的条件为您的状况或布尔属性。然后这样做:

[class.yourClass]="YourCondition"

Let YourCondition be your condition or a Boolean property. Then do it like this:

[class.yourClass]="YourCondition"
梦冥 2025-02-07 04:50:05

当我创建一个反应性形式时,我必须在按钮上分配两种类型的类。这就是我这样做的方式:

<button type="submit" class="btn" [ngClass]="(formGroup.valid)?'btn-info':''" 
[disabled]="!formGroup.valid">Sign in</button>

当表单有效时,按钮具有BTN和BTN级(来自Bootstrap),否则只有BTN类。

While I was creating a reactive form, I had to assign 2 types of class on the button. This is how I did it:

<button type="submit" class="btn" [ngClass]="(formGroup.valid)?'btn-info':''" 
[disabled]="!formGroup.valid">Sign in</button>

When the form is valid, button has btn and btn-class (from bootstrap), otherwise just btn class.

等风来 2025-02-07 04:50:05

我们可以使用以下语法进行类动态。在Angular 2 Plus中,您可以以各种方式执行此操作:

[ngClass]="{'active': arrayData.length && arrayData[0]?.booleanProperty}"
[ngClass]="{'active': step}"
[ngClass]="step== 'step1'?'active':''"
[ngClass]="step? 'active' : ''"

We can make a class dynamic by using the following syntax. In Angular 2 plus, you can do this in various ways:

[ngClass]="{'active': arrayData.length && arrayData[0]?.booleanProperty}"
[ngClass]="{'active': step}"
[ngClass]="step== 'step1'?'active':''"
[ngClass]="step? 'active' : ''"
帥小哥 2025-02-07 04:50:05

该指令以三种不同的方式运行,具体取决于表达式评估的三种类型中的哪个:

  1. 如果表达式评估到字符串,则字符串应为一个或多个空格限制的类名称。
  2. 如果表达式评估对象,则对于具有真实值的对象的每个键值对,相应的键被用作类名称。
  3. 如果表达式评估到数组,则数组的每个元素应为字符串,如类型1中的字符串,或类型2中的对象。这意味着您可以将字符串和对象混合在一起,以使您更加控制什么出现CSS课程。有关此的示例,请参见下面的代码。
    [class.class_one] = "step === 'step1'"

    [ngClass]="{'class_one': step === 'step1'}"

对于多个选项:

    [ngClass]="{'class_one': step === 'step1', 'class_two' : step === 'step2' }" 

    [ngClass]="{1 : 'class_one', 2 : 'class_two', 3 : 'class_three'}[step]"

    [ngClass]="step == 'step1' ? 'class_one' : 'class_two'"

The directive operates in three different ways, depending on which of three types the expression evaluates to:

  1. If the expression evaluates to a string, the string should be one or more space-delimited class names.
  2. If the expression evaluates to an object, then for each key-value pair of the object with a truthy value the corresponding key is used as a class name.
  3. If the expression evaluates to an array, each element of the array should either be a string as in type 1 or an object as in type 2. This means that you can mix strings and objects together in an array to give you more control over what CSS classes appear. See the code below for an example of this.
    [class.class_one] = "step === 'step1'"

    [ngClass]="{'class_one': step === 'step1'}"

For multiple options:

    [ngClass]="{'class_one': step === 'step1', 'class_two' : step === 'step2' }" 

    [ngClass]="{1 : 'class_one', 2 : 'class_two', 3 : 'class_three'}[step]"

    [ngClass]="step == 'step1' ? 'class_one' : 'class_two'"
演出会有结束 2025-02-07 04:50:05

这对我有用:

[ngClass]="{'active': dashboardComponent.selected_menu == 'profile'}"

This is what worked for me:

[ngClass]="{'active': dashboardComponent.selected_menu == 'profile'}"
这样的小城市 2025-02-07 04:50:05

ngclass 语法:

[ngClass]="{'classname' : conditionFlag}"

您可以这样使用:

<ol class="breadcrumb">
  <li [ngClass]="{'active': step==='step1'}" (click)="step='step1'">Step1</li>
  <li [ngClass]="{'active': step==='step2'}" (click)="step='step2'">Step2</li>
  <li [ngClass]="{'active': step==='step3'}" (click)="step='step3'">Step3</li>
</ol>

ngClass syntax:

[ngClass]="{'classname' : conditionFlag}"

You can use it like this:

<ol class="breadcrumb">
  <li [ngClass]="{'active': step==='step1'}" (click)="step='step1'">Step1</li>
  <li [ngClass]="{'active': step==='step2'}" (click)="step='step2'">Step2</li>
  <li [ngClass]="{'active': step==='step3'}" (click)="step='step3'">Step3</li>
</ol>
粉红×色少女 2025-02-07 04:50:05

对于 elseif 语句(较少比较),请这样使用(例如,您比较三个语句):

<div [ngClass]="step === 'step1' ? 'class1' : (step === 'step2' ? 'class2' : 'class3')"> {{step}} </div>

For an elseif statement (less comparison), use it like this (for example, you compare three statements):

<div [ngClass]="step === 'step1' ? 'class1' : (step === 'step2' ? 'class2' : 'class3')"> {{step}} </div>
ぺ禁宫浮华殁 2025-02-07 04:50:05

它与 [ngclass] 指令无关,但我也遇到了与

无法阅读属性“删除”未定义的...

,我认为这是我 [ngclass] 条件中的错误,但事实证明我试图在<的条件下尝试访问的属性代码> [ngclass] 未初始化。

就像我在打字稿文件中

element: {type: string};

以及 [ngclass] 中都有这个,我正在使用

[ngClass]="{'active', element.type === 'active'}"

,并且正在遇到错误

无法在...

上读取未定义的属性“类型”

解决方案是将我的属性修复给

element: {type: string} = {type: 'active'};

我,希望它可以帮助试图在 [ngclass] 中匹配属性条件的人。

It is not relevant with the [ngClass] directive, but I was also getting the same error as

Cannot read property 'remove' of undefined at...

and I thought it to be the error in my [ngClass] condition, but it turned out the property I was trying to access in the condition of [ngClass] was not initialized.

Like I had this in my TypeScript file

element: {type: string};

and in my [ngClass], I was using

[ngClass]="{'active', element.type === 'active'}"

And I was getting the error

Cannot read property 'type' of undefined at...

and the solution was to fix my property to

element: {type: string} = {type: 'active'};

I hope it helps somebody who is trying to match a condition of a property in [ngClass].

难以启齿的温柔 2025-02-07 04:50:05

该示例有点大,但是触发类而不是键入内联的是我的第一个首选方法。
这样,您可以将尽可能多的可能性添加到元素中。
对于那些想要将多个[ngclass]绑定到一个元素的人可能有一种方法。

<span class="inline-flex items-center font-medium" [ngClass]="addClass">{{ badge.text }}</span>

import { ChangeDetectionStrategy, Component, Input } from '@angular/core';

type Badge = {
    size?: 'basic' | 'large';
    shape?: 'basic' | 'rounded';
    color?: 'gray' | 'red' | 'yellow' | 'green' | 'blue' | 'indigo' | 'purple' | 'pink';
    dot?: boolean;
    removeButton?: false;
    text?: string;
}

@Component({
    selector: 'bio-badge',
    templateUrl: './badge.component.html',
    styleUrls: ['./badge.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BioBadgeComponent {
    @Input() badge!: Badge;
    get addClass() {
        return {
            'px-2.5 py-0.5 text-sx': this.badge.size === 'basic',
            'px-3 py-0.5 text-sm': this.badge.size === 'large',
            'rounded-full': this.badge.shape === 'basic',
            'rounded': this.badge.shape === 'rounded',
            'bg-gray-100 text-gray-800': this.badge.color === 'gray',
            'bg-red-100 text-red-800': this.badge.color === 'red',
            'bg-yellow-100 text-yellow-800': this.badge.color === 'yellow',
            'bg-green-100 text-green-800': this.badge.color === 'green',
            'bg-blue-100 text-blue-800': this.badge.color === 'blue',
            'bg-indigo-100 text-indigo-800': this.badge.color === 'indigo',
            'bg-purple-100 text-purple-800': this.badge.color === 'purple',
            'bg-pink-100 text-pink-800': this.badge.color === 'pink',
        }
    }
}

The example is a bit big, but triggering a class instead of typing inline is my first preferred approach.
This way, you can add as many possibilities as you want to your element.
There may be a way for those who want to bind more than one [ngClass] to a single element.

<span class="inline-flex items-center font-medium" [ngClass]="addClass">{{ badge.text }}</span>

import { ChangeDetectionStrategy, Component, Input } from '@angular/core';

type Badge = {
    size?: 'basic' | 'large';
    shape?: 'basic' | 'rounded';
    color?: 'gray' | 'red' | 'yellow' | 'green' | 'blue' | 'indigo' | 'purple' | 'pink';
    dot?: boolean;
    removeButton?: false;
    text?: string;
}

@Component({
    selector: 'bio-badge',
    templateUrl: './badge.component.html',
    styleUrls: ['./badge.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BioBadgeComponent {
    @Input() badge!: Badge;
    get addClass() {
        return {
            'px-2.5 py-0.5 text-sx': this.badge.size === 'basic',
            'px-3 py-0.5 text-sm': this.badge.size === 'large',
            'rounded-full': this.badge.shape === 'basic',
            'rounded': this.badge.shape === 'rounded',
            'bg-gray-100 text-gray-800': this.badge.color === 'gray',
            'bg-red-100 text-red-800': this.badge.color === 'red',
            'bg-yellow-100 text-yellow-800': this.badge.color === 'yellow',
            'bg-green-100 text-green-800': this.badge.color === 'green',
            'bg-blue-100 text-blue-800': this.badge.color === 'blue',
            'bg-indigo-100 text-indigo-800': this.badge.color === 'indigo',
            'bg-purple-100 text-purple-800': this.badge.color === 'purple',
            'bg-pink-100 text-pink-800': this.badge.color === 'pink',
        }
    }
}
青衫负雪 2025-02-07 04:50:05

使用:

<div class="collapse in " [ngClass]="(active_tab=='assignservice' || active_tab=='manage')?'show':''" id="collapseExampleOrganization" aria-expanded="true" style="">
    <ul>
        <li class="nav-item" [ngClass]="{'active': active_tab=='manage'}">
            <a routerLink="/main/organization/manage" (click)="activemenu('manage')"> <i class="la la-building-o"></i>
               <p>Manage</p></a></li>
        <li class="nav-item" [ngClass]="{'active': active_tab=='assignservice'}"><a routerLink="/main/organization/assignservice" (click)="activemenu('assignservice')"><i class="la la-user"></i><p>Add organization</p></a></li>
    </ul>
</div>

该代码是ngclass 的一个很好的例子,如果 - else 条件。

[ngClass]="(active_tab=='assignservice' || active_tab=='manage')?'show':''"

[ngClass]="{'active': active_tab=='assignservice'}"

Use:

<div class="collapse in " [ngClass]="(active_tab=='assignservice' || active_tab=='manage')?'show':''" id="collapseExampleOrganization" aria-expanded="true" style="">
    <ul>
        <li class="nav-item" [ngClass]="{'active': active_tab=='manage'}">
            <a routerLink="/main/organization/manage" (click)="activemenu('manage')"> <i class="la la-building-o"></i>
               <p>Manage</p></a></li>
        <li class="nav-item" [ngClass]="{'active': active_tab=='assignservice'}"><a routerLink="/main/organization/assignservice" (click)="activemenu('assignservice')"><i class="la la-user"></i><p>Add organization</p></a></li>
    </ul>
</div>

The code is a good example of an ngClass if-else condition.

[ngClass]="(active_tab=='assignservice' || active_tab=='manage')?'show':''"

[ngClass]="{'active': active_tab=='assignservice'}"
偏闹i 2025-02-07 04:50:05

尝试这样...

用''定义您的班级:

<ol class="breadcrumb">
    <li *ngClass="{'active': step==='step1'}" (click)="step='step1; '">Step1</li>
    <li *ngClass="{'active': step==='step2'}"  (click)="step='step2'">Step2</li>
    <li *ngClass="{'active': step==='step3'}" (click)="step='step3'">Step3</li>
</ol>

Try it like this...

Define your class with '':

<ol class="breadcrumb">
    <li *ngClass="{'active': step==='step1'}" (click)="step='step1; '">Step1</li>
    <li *ngClass="{'active': step==='step2'}"  (click)="step='step2'">Step2</li>
    <li *ngClass="{'active': step==='step3'}" (click)="step='step3'">Step3</li>
</ol>
渔村楼浪 2025-02-07 04:50:05

如果用户想根据&amp;&amp; and || 的类别显示课程,那么以下一个对我有用:

[ngClass]="{'clasname_1':  condition_1 && condition_2, 'classname_2':  condition_1 && condition2, 'classname_3': condition}"

示例:示例:

[ngClass]="{'approval-panel-mat-drawer-side-left':  similar_toil_mode==='side' && showsTheSimilarToilsWithCloseIcon, 'approval-panel-mat-drawer-side-right':  similar_toil_mode==='side' && !showsTheSimilarToilsWithCloseIcon, 'approval-panel-mat-drawer-over': similar_toil_mode==='over'}"

If a user wants to display the class on basis of && and ||, then the below one is working for me:

[ngClass]="{'clasname_1':  condition_1 && condition_2, 'classname_2':  condition_1 && condition2, 'classname_3': condition}"

Example:

[ngClass]="{'approval-panel-mat-drawer-side-left':  similar_toil_mode==='side' && showsTheSimilarToilsWithCloseIcon, 'approval-panel-mat-drawer-side-right':  similar_toil_mode==='side' && !showsTheSimilarToilsWithCloseIcon, 'approval-panel-mat-drawer-over': similar_toil_mode==='over'}"
等数载,海棠开 2025-02-07 04:50:05

的一些有用的助手管

@Pipe({
    name: 'condition'
})
export class ONgConditionPipe implements PipeTransform {
    transform(value: any, condition?: any): any {
        if (condition != null) {
            return value;
        }

        if (value) {
            return value;
        }
    }
}

@Pipe({
    name: 'default'
})
export class ONgDefaultPipe implements PipeTransform {
    transform(value: any, defaultValue?: any): any {
        return value ?? defaultValue;
    }
}

NGCLASS示例

[ngClass]="[headerClass | default: '', 'cursor-move' | condition: draggable, ('cursor-move2' | condition: draggable) | default: '']"

Some usefull helper pipes for ngClass

@Pipe({
    name: 'condition'
})
export class ONgConditionPipe implements PipeTransform {
    transform(value: any, condition?: any): any {
        if (condition != null) {
            return value;
        }

        if (value) {
            return value;
        }
    }
}

@Pipe({
    name: 'default'
})
export class ONgDefaultPipe implements PipeTransform {
    transform(value: any, defaultValue?: any): any {
        return value ?? defaultValue;
    }
}

Example

[ngClass]="[headerClass | default: '', 'cursor-move' | condition: draggable, ('cursor-move2' | condition: draggable) | default: '']"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文