Angular 角度4点击+;ngClass,如何仅应用于自身而非ngFor内部的所有元素
在Angular 2中,当我单击按钮时,它在ngFor循环中的每个“按钮”中添加了类“newstyle”。。。如何仅将ngClass条件绑定到元素本身Angular 角度4点击+;ngClass,如何仅应用于自身而非ngFor内部的所有元素,angular,Angular,在Angular 2中,当我单击按钮时,它在ngFor循环中的每个“按钮”中添加了类“newstyle”。。。如何仅将ngClass条件绑定到元素本身 <button [ngClass]="{'newstyle': isClicked }" (click)=" isClicked = !isClicked">Click button</button> 点击按钮 谢谢大家! 试试这个: <button *ngFor="let thing of things" [c
<button [ngClass]="{'newstyle': isClicked }" (click)=" isClicked = !isClicked">Click button</button>
点击按钮
谢谢大家! 试试这个:
<button *ngFor="let thing of things" [class.newstyle]="currentThing === thing" (click)="currentThing = thing">Click button</button>
试试这个:
<button *ngFor="let thing of things" [class.newstyle]="currentThing === thing" (click)="currentThing = thing">Click button</button>
一个类似的用例:在集合中迭代,并为
collection
中编辑的items
应用一个特定的类item updated
。此部分是可选的,仅用于演示:
[attr.title]=“item.isDirty?”项已更新:null“
html组件
<div *ngFor="let item of collection">
<label>Name: {{item.name}}</label>
<button class="btn"
[ngClass]="{'item-updated' : item.isDirty}"
[attr.title]="item.isDirty ? 'Item was updated' : null">
Save
</button>
</div>
<div *ngFor="let item of yourArray">
<label *ngIf="item.isClicked">Item was Clicked: {{item.id}}</label>
<button [ngClass]="{'newStyle' : !item.isClicked}" (click)="changeItemState(item)">Click button </button>
</div>
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'your-component',
templateUrl: './your.component.html'
})
export class YourComponent implements OnInit {
public yourArray: any[];
public ngOnInit(): void {
this.yourArray = [
{
id: 1,
name: 'First',
isClicked: false;
},
{
id: 2,
name: 'Second',
isClicked: false;
},
{
id: 3,
name: 'Third',
isClicked: true;
},
];
}
public changeItemState(item: any): void {
item.isClicked = !item.isClicked;
}
}
已转换
HTML组件
<div *ngFor="let item of collection">
<label>Name: {{item.name}}</label>
<button class="btn"
[ngClass]="{'item-updated' : item.isDirty}"
[attr.title]="item.isDirty ? 'Item was updated' : null">
Save
</button>
</div>
<div *ngFor="let item of yourArray">
<label *ngIf="item.isClicked">Item was Clicked: {{item.id}}</label>
<button [ngClass]="{'newStyle' : !item.isClicked}" (click)="changeItemState(item)">Click button </button>
</div>
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'your-component',
templateUrl: './your.component.html'
})
export class YourComponent implements OnInit {
public yourArray: any[];
public ngOnInit(): void {
this.yourArray = [
{
id: 1,
name: 'First',
isClicked: false;
},
{
id: 2,
name: 'Second',
isClicked: false;
},
{
id: 3,
name: 'Third',
isClicked: true;
},
];
}
public changeItemState(item: any): void {
item.isClicked = !item.isClicked;
}
}
一个类似的用例:在集合中迭代,并为
collection
中编辑的items
应用一个特定的类item updated
。此部分是可选的,仅用于演示:
[attr.title]=“item.isDirty?”项已更新:null“
html组件
<div *ngFor="let item of collection">
<label>Name: {{item.name}}</label>
<button class="btn"
[ngClass]="{'item-updated' : item.isDirty}"
[attr.title]="item.isDirty ? 'Item was updated' : null">
Save
</button>
</div>
<div *ngFor="let item of yourArray">
<label *ngIf="item.isClicked">Item was Clicked: {{item.id}}</label>
<button [ngClass]="{'newStyle' : !item.isClicked}" (click)="changeItemState(item)">Click button </button>
</div>
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'your-component',
templateUrl: './your.component.html'
})
export class YourComponent implements OnInit {
public yourArray: any[];
public ngOnInit(): void {
this.yourArray = [
{
id: 1,
name: 'First',
isClicked: false;
},
{
id: 2,
name: 'Second',
isClicked: false;
},
{
id: 3,
name: 'Third',
isClicked: true;
},
];
}
public changeItemState(item: any): void {
item.isClicked = !item.isClicked;
}
}
已转换
HTML组件
<div *ngFor="let item of collection">
<label>Name: {{item.name}}</label>
<button class="btn"
[ngClass]="{'item-updated' : item.isDirty}"
[attr.title]="item.isDirty ? 'Item was updated' : null">
Save
</button>
</div>
<div *ngFor="let item of yourArray">
<label *ngIf="item.isClicked">Item was Clicked: {{item.id}}</label>
<button [ngClass]="{'newStyle' : !item.isClicked}" (click)="changeItemState(item)">Click button </button>
</div>
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'your-component',
templateUrl: './your.component.html'
})
export class YourComponent implements OnInit {
public yourArray: any[];
public ngOnInit(): void {
this.yourArray = [
{
id: 1,
name: 'First',
isClicked: false;
},
{
id: 2,
name: 'Second',
isClicked: false;
},
{
id: 3,
name: 'Third',
isClicked: true;
},
];
}
public changeItemState(item: any): void {
item.isClicked = !item.isClicked;
}
}
您的
for
循环是什么样子的?你能在它里面创造一些条件,只满足你想要应用类样式的单个按钮吗?我的问题与此类似:但新的答案不适用于Angular 4。我不知道为什么:单击按钮我在你的评论中对你发布的问题添加了一个更一般的答案,因为被接受的答案质量很低。你的for
循环是什么样子的?你能在它里面创造一些条件,只满足你想要应用类样式的单个按钮吗?我的问题与此类似:但新的答案不适用于Angular 4。我不知道为什么:点击按钮我为你在评论中发布的问题添加了一个更一般的答案,因为被接受的答案质量很低。太棒了!非常感谢。你知道如何用*ngIf做同样的事情吗?示例:单击按钮导出组件{isShown:boolean[];}@TwikssPower,也请参见我的答案中的*ngIf
:项目被单击:{{Item.id}
。可以以相同的方式应用于任何html元素。祝你好运令人惊叹的!非常感谢。你知道如何用*ngIf做同样的事情吗?示例:单击按钮导出组件{isShown:boolean[];}@TwikssPower,也请参见我的答案中的*ngIf
:项目被单击:{{Item.id}
。可以以相同的方式应用于任何html元素。祝你好运