【发布时间】:2021-05-01 15:34:55
【问题描述】:
我是 Angular CLI 的新手,我正在使用 v11。我正在尝试创建一个图像库,单击某些图像会生成一个模式,其中一个轮播在开头显示所选图像。但是当我单击图像并出现模式时,由于某种原因@ViewChild 装饰器,它总是返回未定义。
我的模板代码如下:
<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">×</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;
}
}
感谢您的帮助。
【问题讨论】:
-
不,它没有。我已经尝试过类似的方法,但
this.myCarousel总是未定义。
标签: angular bootstrap-modal viewchild bootstrap-carousel angular-cli-v11