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

I am new to Angular CLI and I am using v11. I am trying to create an image gallery that clicking on some image generates a modal with a carousel showing the selected image at the beginning. But when I click on the image and the modal appears, for some reason the @ViewChild decorator, it always returns undefined.

My template code is the following:

    <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>
      <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 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="">

My ts code is the following:

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

  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) {

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


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

Thanks for the help.

Source: Angular Questions