[英]Angular 4 on click event change button text and style
I have a component with the following methods 我有一个组件与以下方法
onProductAdded(product: ImportList) { // Add an Item to ImportList
// after some logic add product to ImportList
this.productIsOnImportList = true;
console.log('product added');
}
onProductRemoved(product: ImportList) {
this.productIsOnImportList = false;
console.log('product removed');
}
And on the html template i have 在我的html模板上
<button
(click)="onProductAdded(product)"
*ngIf="!productIsOnImportList"
class="ui labeled icon blue button">
<i class="plus icon"></i>
Add to Import List
</button>
<button
(click)="onProductRemoved(product)"
*ngIf="productIsOnImportList"
class="ui labeled icon red button">
<i class="minus icon"></i>
Remove
</button>
The problem is right now the behavior is global, the click affects all products but i want the click to be private to individual product. 问题是现在行为是全局的,点击会影响所有产品,但我希望点击对个别产品是私有的。 How can I achieve this?
我怎样才能做到这一点?
You can simply using $event
to achieve this instead of having two separate buttons 您可以简单地使用
$event
来实现此目的,而不是使用两个单独的按钮
onProductAdded(event){
if(event.srcElement.innerHTML ==='Add to Import List' ){
//// perform add action
event.srcElement.innerHTML="Remove";
} else if(event.srcElement.innerHTML ==='Remove'){
//// perform remove action
event.srcElement.innerHTML="Add to Import List";
}
}
HTML HTML
<button (click)="onProductAdded($event)">Add to Import List</button>
Update 1 : Based on comment for the font-awesome icon 更新1:基于font-awesome图标的评论
onProductAdded(event){
if(event.srcElement.childNodes[1].textContent === 'Add to Import List' ){
//// perform add action
event.srcElement.childNodes[0].classList.remove('fa-plus');
event.srcElement.childNodes[0].classList.add('fa-times');
event.srcElement.childNodes[1].textContent="Remove";
} else if(event.srcElement.innerText ==='Remove'){
//// perform remove action
event.srcElement.childNodes[0].classList.add('fa-plus');
event.srcElement.childNodes[0].classList.remove('fa-times');
event.srcElement.childNodes[1].textContent="Add to Import List";
}
}
Note : Live Demo is also updated. 注意:现场演示也会更新。
I resolved it by adding a boolean variable to my model class. 我通过向模型类添加一个布尔变量来解决它。
export interface ImportList {
id: number;
added?: boolean;
}
then on the component class I changed the methods to 然后在组件类我改变了方法
onProductAdded(product: ImportList) { // Add an Item to ImportList
// after some logic add product to ImportList
//this.importList.push(product);
product.added = true;
console.log('product added');
}
onProductRemoved(product: ImportList) {
product.added = false;
console.log('product removed');
}
And in the template i did this. 在模板中我做到了这一点。
<button
(click)="onProductAdded(product)"
*ngIf="!product.added"
class="ui labeled icon blue button">
<i class="plus icon"></i>
Add to Import List
</button>
<button
(click)="onProductRemoved(product)"
*ngIf="product.added"
class="ui labeled icon red button">
<i class="minus icon"></i>
Remove
</button>
By doing this I was able to listen to clicks on individual products. 通过这样做,我能够听取单个产品的点击。 I hope this helps someone.
我希望这可以帮助别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.