简体   繁体   English

子组件的 NgClass 未触发

[英]NgClass of child component not triggered

I have a component for a gallery carousel with navigation for the images below it.我有一个用于画廊轮播的组件,并为它下面的图像提供导航。 Clicking on an image in the navigation bellow should change the image displayed on the carousel to the one selected (this works) and add a border around the selected image in the navigation (this does not).单击导航波纹管中的图像应将轮播上显示的图像更改为选定的图像(这可行),并在导航中的选定图像周围添加边框(这不会)。 I've tried to add the border by appending a style class to the HTML element through ngClass.我尝试通过 ngClass 将样式 class 附加到 HTML 元素来添加边框。 I keep track of the index of the selected image and when rendering the images in the navigation if the index of the selected image is the same as the index of the rendered image the border is then added.我跟踪所选图像的索引,并且在导航中渲染图像时,如果所选图像的索引与渲染图像的索引相同,则添加边框。 However, I assume that the elements are not rerendered because after I click on an image the border is removed from the previous image and it is not added to the new one as it should.但是,我假设元素不会重新渲染,因为在我单击图像后,边框会从前一个图像中删除,并且不会按应有的方式添加到新图像中。 Also, clicking on the same image after a second time adds the border (I think that this is when the child component renders once again).此外,第二次单击同一图像会添加边框(我认为这是子组件再次呈现的时候)。 What is the issue here that the ngClass is ignored the first time?第一次忽略 ngClass 的问题是什么?

HTML of image navigation:图像导航的 HTML:

<div *ngIf="showImageList" class="product_gallery_images mobile_hidden">
  <div *ngFor="let img of imgList; index as index" class="product_gallery_image". 
(click)="selectImage(index)">
    <img [ngClass]="{'selected_image': selectedImage === index}"  src=". 
{{img.fields.file.url}}" [id]="index.toString()">
  </div>
</div>

selectImage function:选择图像 function:

selectImage(index) {
console.log(document.getElementById(this.selectedImage.toString()).classList);
document.getElementById(index.toString()).classList.add('selected_image');
document.getElementById(this.selectedImage.toString()).classList.remove('selected_image');
this.selectedImage = index;
this.changeImage.changeImage(index.toString());
}

I have tried to directly add and remove the class from the classList of the element but this did not work as well.我试图从元素的 classList 中直接添加和删除 class 但这也不起作用。 I have similar functionality of the same page where the border is rendered without any issues and without the need to dynamically altering the classlist.我具有相同页面的类似功能,其中边框呈现没有任何问题,并且无需动态更改类列表。 The difference here is that this is not in a child component.这里的区别在于它不在子组件中。

First click on the fourth image in navigation:首先点击导航中的第四张图片:

在此处输入图像描述

A second click on the fourth image in navigation:第二次点击导航中的第四张图片:

在此处输入图像描述

I think the let keyword is missing:我认为let关键字丢失了:

*ngFor="let img of imgList; let index = index"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 堆叠的父子组件未触发子组件的onClick事件 - onClick event of child component is not getting triggered for stacked parent and child component 停止从子组件触发父组件波纹 - Stop parent component ripple being triggered from child component 子组件发出自定义事件,但未触发父组件的侦听器 - child component emit an custom event, but parent component's listener not triggered VueJS:在子组件中触发事件时,在父组件中运行功能 - VueJS: Run function in parent component when event triggered in child component 由子组件中的事件处理程序触发时,useState 元素不会重新呈现 - useState element not rerendering when triggered by an event handler in a child component 如果使用 *ngIf 删除子组件,则会触发 Angular 自定义点击监听器 - Angular custom clickaway listener is triggered if a child component is removed using *ngIf 高阶函数渲染两次,导致子组件被触发 - higher order function render twice causing child component to be triggered 在 React 的父组件中触发动作时获取子状态 - Get child state when the action is triggered in parent component in React ionic 3,自定义组件和ngClass绑定问题 - ionic 3, custom component and ngClass binding issue Angular 6 [ngClass]在component.js中不使用boolean - Angular 6 [ngClass] not working with boolean in component.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM