Angular6 创建EmbeddedView时获取不同的上下文变量

Angular6 创建EmbeddedView时获取不同的上下文变量,angular6,Angular6,在这段代码中,我创建了一个嵌入式视图并传递了一个上下文。嵌入的视图是图像缩略图 const thumbnailContext = new ThumbnailContext(new ImageContext(divId, buttonId, imgId, closeId, imageString, this.thumbnailContainerRef.length, null)); // viewref is empty now. It w

在这段代码中,我创建了一个嵌入式视图并传递了一个上下文。嵌入的视图是图像缩略图

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>) {} //TODOM - not sure if the types are correct
}
export class ThumbnailContext {
  constructor(public context: ImageContext) {}
}
视图在这里嵌入

<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>
我想我应该在
{context:{“divId”:“thumbnail-1”,“buttonnid”:“thumbnail-button-1”,“imgId”:“img-1”,“closeId”:“close-button-1”,“imgSrc”:“}}

我怀疑在
let context=“context”
中,上下文变量被映射到
Thumbnail
类的
context
属性。正确吗

export class ThumbnailContext {
  constructor(public context: ImageContext) {}
}

如何将传递的上下文映射到
ThumbnailContext

我对
let-
指令如何工作的理解是不正确的“然后
上下文
是传递给模板的对象中的一个键。此键的值在模板中以
something
的形式提供

从SO的其他答案中选择,有两种方法可以将上下文传递给
ng模板
。为Angular的
$implicit
赋值,或者在对象中创建一个新的
,并引用该
如果你添加了一个模板

{{col}} {{foo}}

如果传递的对象是{$implicit:'World',bar:'Svet'};然后
col
将被分配值
{world}
,因为它被分配了
$implicit
foo
是条形的赋值,即
Svet
,因为
foo
等于传递对象的键
bar
,并且
bar
的值是
Svet

对于
let col
,上下文属性
$implicit
在绑定模板中作为
col
提供,即angular正在创建一个对象
{col:'World'}
,这使得代码等同于
let col=“col”
。所以
col
映射到值为
world
的键
col

因此,如果我在html中使用
let mycontext=“context”
,那么我需要传递的对象中的
context
属性,在我的例子中就是这样。我正在传递对象
ThumbnailContext
,它有一个键
context

export class ThumbnailContext { constructor(public context: ImageContext) {} }
因此,模板内部的
mycontext
实际上是
ImageContext
,它是模板中的本地名称

由于我在模板中有可用的
ImageContext
,并且我正在将其传递给
deletehumbnail
,因此我还必须更改
deletehumbnail
deletehumbnail(thumbnailContext:ImageContext){…

export class ThumbnailContext {
  constructor(public context: ImageContext) {}
}
export class ThumbnailContext { constructor(public context: ImageContext) {} }