在这段代码中,我正在创建一个嵌入式视图并传递一个上下文。嵌入的视图是图像缩略图
const thumbnailContext = new ThumbnailContext(new ImageContext(divId,
buttonId,
imgId,
closeId,
imageString, this.thumbnailContainerRef.length, null));
// viewref is empty now. It will contain reference of this created view (see below)
console.log('uploading context ',thumbnailContext);
thumbnailTemplateViewRef = this.thumbnailContainerRef.createEmbeddedView(this.thumbnailTemplateRef, thumbnailContext);
类定义如下
export class ImageContext {
constructor(public divId: string,
public buttonId: string,
public imgId: string,
public closeId: string,
public imgSrc: string,
public index: number,
public viewRefId: EmbeddedViewRef<ThumbnailContext>) {}
}
export class ThumbnailContext {
constructor(public context: ImageContext) {}
}
据我所知,控制台打印正确
ThumbnailContext {context: ImageContext}
context: ImageContext
divId: "thumbnail-1"
buttonId: "thumbnail-button-1"
imgId: "img-1"
closeId: "close-button-1"
imgSrc: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjUA"
index: 0
viewRefId: null
视图嵌入在这里
<ng-template #thumbnailTemplate let-context="context">
<div id="{{context.divId}}">
<img id="{{context.imgId}}" src="{{context.imgSrc}}">
<a href="javascript:void(0)" id="{{context.closeId}}" (click)="this.deleteThumbnail(context)"></a>
</div>
</ng-template>
图像创建正确。但当我尝试使用删除它们时
deleteThumbnail
并将上下文传递给它,我得到了一个不正确的上下文
deleteThumbnail(thumbnailContext: ThumbnailContext) {
console.log("delete thumbnail clicked with context ",JSON.stringify(thumbnailContext));
const index = thumbnailContext.context.index;
..
}
delete thumbnail clicked with context {"divId":"thumbnail-1","buttonId":"thumbnail-button-1","imgId":"img-1","closeId":"close-button-1","imgSrc":"..."}
我想我应该得到一个带有上下文对象的对象
{context:{"divId":"thumbnail-1","buttonId":"thumbnail-button-1","imgId":"img-1","closeId":"close-button-1","imgSrc":"}}
我的怀疑是
let-context="context"
,上下文变量被映射到
context
财产
Thumbnail
类。正确吗?
export class ThumbnailContext {
constructor(public context: ImageContext) {}
}
如何将传递的上下文映射制作为
ThumbnailContext
?