admin管理员组文章数量:1431398
List of images are executing in a loop(ngFor). I am trying to get the current element that is being clicked by calling the function (click)="addStyle()".The issue I am facing here is ,if i am adding a style [class.active] depending upon the clicked method,it gets reflected in all the image tags.But I am trying to add style to only the particular image being clicked.
ponent.html
<div class="item" *ngFor="let i of img;let i = index">
<img class="product-i-2" [src]="i" [class.active]="isActive" (click)="addStyle()">
</div>
ponent.ts
isActive:false;
addStyle()
{
this.isActive=true;
}
ponent.css
.active
{
border:1px solid red;
}
In short,I am trying to add style to only particular image tag and not all the images in the loop
List of images are executing in a loop(ngFor). I am trying to get the current element that is being clicked by calling the function (click)="addStyle()".The issue I am facing here is ,if i am adding a style [class.active] depending upon the clicked method,it gets reflected in all the image tags.But I am trying to add style to only the particular image being clicked.
ponent.html
<div class="item" *ngFor="let i of img;let i = index">
<img class="product-i-2" [src]="i" [class.active]="isActive" (click)="addStyle()">
</div>
ponent.ts
isActive:false;
addStyle()
{
this.isActive=true;
}
ponent.css
.active
{
border:1px solid red;
}
In short,I am trying to add style to only particular image tag and not all the images in the loop
Share Improve this question asked May 9, 2019 at 5:42 Sathya Narayanan GVKSathya Narayanan GVK 9492 gold badges17 silver badges33 bronze badges 3-
1
(click)="addStyle(i)"
add i to addStyle, andclass.active
should beimg.active
. Renamelet i = index
tolet _i = index
to avoid conflicts with the already declared variablei
. AlteraddStyle
to seti.isActive
to!i.isActive
. – briosheje Commented May 9, 2019 at 5:44 - thanks for the help..but i am still trying to add style to only the particular element that is being clicked – Sathya Narayanan GVK Commented May 9, 2019 at 5:50
- This will do that. You just need to disable the style on the other elements. – briosheje Commented May 9, 2019 at 5:53
5 Answers
Reset to default 2Try this one.
<div class="item" *ngFor="let i of img;let i = index">
<img class="product-i-2" [src]="i" [class.active]="i === activeIdx"
(click)="addStyle(i)">
</div>
activeIdx: number;
addStyle(i)
{
this.activeIdx = i;
}
you can try like this
HTML
<div class="item" *ngFor="let i of img;let j = index">
<img class="product-i-2" [src]="i" [class.active]="isActive[i] == true ? 'active' : '' " (click)="addStyle(j)">
</div>
TS
isActive: any[] = false;
ngOnInit() {
// here we set defalut value to false
for (let i = 0; i < img.length; i++) {
this.isActive.push(false);
}
}
addStyle(j)
{
this.isActive[j]= !this.isActive[j];
}
You can pass event parameter on click and in that you will get element and than you can add class into it.
<div class="item" *ngFor="let i of img;let i = index">
<img class="product-i-2" [src]="i" (click)="addStyle(e)">
</div>
// In TS file
addStyle(event)
{
event.target.classList.add(className);
}
The best way is you convert the array of images to array of object of images and mark as active.
ngOnInit(){
this.img.forEach((img)=>{
this.alter.push({src:img, isActive:false})
})
}
addAlterStyle(index){
this.alter.forEach((img) => img.isActive = false);
this.alter[index].isActive = true;
}
HTML
<div class="item" *ngFor="let image of alter;let i = index">
<img class="product-i-2" [ngClass]="{'isActive':image.isActive}" [src]="image.src" (click)="addAlterStyle(i)">
</div>
If you dont want to change the img array structure
TS
addStyle(event){
let imageTags = document.querySelectorAll('.image');
imageTags.forEach((element)=>{
element.classList.remove('isActive')
})
event.target.classList.add('isActive')
}
HTML
<div class="item" *ngFor="let image of img;let i = index">
<img class="product-i-2 image" [src]="image" (click)="addStyle($event)">
</div>
Here is the stackblitz working demo
don't save isActive property save active Image and then you can check a condition by reference
activeImg:any;
addStyle($event){
this.activeImg=$event;
}
<div class="item" *ngFor="let i of img;let i = index">
<img class="product-i-2" [src]="i" [class.active]="activeImg==i" (click)="addStyle()">
本文标签: javascriptGet the current element running in ngForStack Overflow
版权声明:本文标题:javascript - Get the current element running in ngFor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745571823a2664092.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论