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