@ViewChild returns 未定义。我无法访问模式内的轮播 ElementRef

@ViewChild returns undefined. I can not access to the carousel ElementRef inside a modal

我是 Angular CLI 的新手,我使用的是 v11。我正在尝试创建一个图像库,单击某些图像会生成一个带有轮播的模态,该模式在开头显示所选图像。但是当我点击图像并出现模态时,出于某种原因 @ViewChild 装饰器,它总是 returns undefined.

我的模板代码如下:

<mat-card>
  <mat-card-subtitle>Gallery</mat-card-subtitle>
  <mat-card-content>
    <ng-template id="mySlides" #myModal class="h-100 h-auto" let-d="dismiss">
      <div class="modal-header">
        <h4 class="modal-title">Gallery</h4>
        <button type="button" class="close mt-1" aria-label="Close" (click)="d('Cross click')">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-content">
        <ngb-carousel #myCarousel>
          <ng-template ngbSlide *ngFor="let img of images" id="{{img.id}}">
            <div class="picsum-img-wrapper">
              <img src="{{img.url}}" alt="Image {{img.id}}">
            </div>
          </ng-template>
        </ngb-carousel>
      </div>
    </ng-template>
    <div class="row text-center text-lg-left">
      <div class="col-lg-3 col-md-4 col-6" *ngFor="let img of images">
        <a style="cursor: pointer" class="d-block mb-4 h-100"
           (click)="openModal(myModal); setSlideId(img.id);navigateToSlide(img.id)">
          <img class="img-fluid img-thumbnail" src="{{img.url}}" alt="">
        </a>
      </div>
    </div>
  </mat-card-content>
</mat-card>

我的ts代码如下:

import {Component, ViewChild} from '@angular/core';
import {NgbCarousel, NgbCarouselConfig, NgbModal} from '@ng-bootstrap/ng-bootstrap';


@Component({
  selector: 'kt-image-gallery-thumbnail',
  templateUrl: './image-gallery-thumbnail.component.html',
  styleUrls: ['./image-gallery-thumbnail.component.scss'],
  providers: [NgbCarouselConfig]

})

export class ImageGalleryThumbnailComponent {

  selectedSlide = 1;

  constructor(private modalService: NgbModal) {
  }

  images = [{
    id: 1,
    url: '../../../../assets/media/products/product1.jpg'
  }, {
    id: 2,
    url: '../../../../assets/media/products/product12.jpg'
  }, {
    id: 3,
    url: '../../../../assets/media/products/product11.jpg'
  }];

  @ViewChild('myCarousel', {static: false, read: NgbCarousel}) myCarousel: NgbCarousel;

  openModal(content: any) {
    this.modalService.open(content);
  }

  navigateToSlide(item) {
    console.log(this.myCarousel);
    try {
      this.myCarousel.select('' + item);
    } catch (e) {
      console.log(e);
    }

  }

  setSlideId(id: number) {
    console.log(id);
    this.selectedSlide = id;
  }
}

感谢您的帮助。

从模板中删除 #myCarousel

然后在你的打字稿文件中简单使用

@ViewChild(NgbCarousel) 
myCarousel: NgbCarousel;

编辑:阅读整个模板我发现主要的结构问题,或者我遗漏了什么。

       <div class="row text-center text-lg-left">
            <div class="col-lg-3 col-md-4 col-6" *ngFor="let img of 
           images">
           <a style="cursor: pointer" class="d-block mb-4 h-100"
          (click)="openModal(myModal); 
          setSlideId(img.id);navigateToSlide(img.id)">
        <img class="img-fluid img-thumbnail" src="{{img.url}}" alt="">
        </a>
         </div>
    </div>

1)您希望如何在 setSlide(img.id) 和 navigateToSlide(img.id) 中获得 img.id?你已经在 ngFor 之外了。此信息已丢失。

2) 我看到在一个组件中您声明了所有模态以及调用它的代码。考虑将所有模态内容移动到一个单独的组件中,并在此处仅保留它表示为没有模态的页面的内容。然后一切都会更清楚,让您检查为什么某些东西不起作用。

3)我认为在所有参考资料中,您都尝试了一种聪明的方法来绕过应该实现的更复杂的代码。如您所见,ngbCarousel 仅属于模态内部,因此它仅在模态内部而不在模态外部起作用是正确的。如果你清除你的结构,也许你可以准确地理解是什么中断了以及为什么。

我终于根据 Panagiotis Bougioukos' 的建议让它工作,我认为它比我所做的更简单。解决方案是下一个:

  1. 我用轮播创建了一个新组件并从模态调用它。

组件库-轮播

HTML 模板

<ngb-carousel #myCarousel [activeId]="item">
  <ng-template ngbSlide *ngFor="let img of images" id="{{img.id}}">
    <div class="picsum-img-wrapper">
      <img src="{{img.url}}" alt="Image {{img.id}}">
    </div>
  </ng-template>
</ngb-carousel>

TS代码

import {Component, Input, ViewChild} from '@angular/core';
import {NgbCarousel} from '@ng-bootstrap/ng-bootstrap';


@Component({
  selector: 'kt-gallery-carousel',
  templateUrl: './gallery-carousel.component.html',
  styleUrls: ['./gallery.carousel.component.scss'],
})

export class GalleryCarouselComponent {

  @Input() item: string;

  @ViewChild('myCarousel', {static: true, read: NgbCarousel}) myCarousel: NgbCarousel;

  constructor() {
  }

  images = [{
    id: 1,
    url: '../../../../assets/media/products/product1.jpg'
  }, {
    id: 2,
    url: '../../../../assets/media/products/product12.jpg'
  }, {
    id: 3,
    url: '../../../../assets/media/products/product11.jpg'
  }];
}
  1. 我修改了我的图片库组件

带模态的组件图库

HTML 模板

<mat-card>
  <mat-card-subtitle>Gallery</mat-card-subtitle>
  <mat-card-content>
    <ng-template id="mySlides" #myModal class="h-100 h-auto" let-d="dismiss">
      <div class="modal-header">
        <h4 class="modal-title">Gallery</h4>
        <button type="button" class="close mt-1" aria-label="Close" (click)="d('Cross click')">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-content">
        <kt-gallery-carousel [item]="selectedSlide"></kt-gallery-carousel>
      </div>
    </ng-template>
    <div class="row text-center text-lg-left">
      <div class="col-lg-3 col-md-4 col-6" *ngFor="let img of images">
        <a style="cursor: pointer" class="d-block mb-4 h-100"
           (click)="openModal(myModal); setSlideId(img.id)">
          <img class="img-fluid img-thumbnail" src="{{img.url}}" alt="">
        </a>
      </div>
    </div>
  </mat-card-content>
</mat-card>

TS代码

import {Component} from '@angular/core';
import {NgbModal} from '@ng-bootstrap/ng-bootstrap';


@Component({
  selector: 'kt-image-gallery-thumbnail',
  templateUrl: './image-gallery-thumbnail.component.html',
})

export class ImageGalleryThumbnailComponent {
  selectedSlide: string;

  constructor(private modalService: NgbModal) {
  }

  images = [{
    id: 1,
    url: '../../../../assets/media/products/product1.jpg'
  }, {
    id: 2,
    url: '../../../../assets/media/products/product12.jpg'
  }, {
    id: 3,
    url: '../../../../assets/media/products/product11.jpg'
  }];

  openModal(content: any) {
    this.modalService.open(content);
  }

  setSlideId(id: number) {
    this.selectedSlide = '' + id;
  }
}