【发布时间】:2017-05-07 12:10:28
【问题描述】:
我有一个要向其传递模板的组件。在这个组件内部,我想传递上下文以便显示数据。
@Component({
selector: 'my-component',
providers: [],
template: `
<template [ngTemplateOutlet]="templ" [ngOutletContext]="{isVisible: true}">
</template>
`
})
export class MyElementComponent implements OnInit {
@ContentChild(TemplateRef) templ;
constructor(){}
}
现在在其他组件内部使用组件时:
<my-component>
<template>
{{isVisible ? 'yes!' : 'no'}}
</template>
</my-component>
所以在my-component 中,我传递了一个模板,该模板由@ContentChild 在其类中处理,名称为templ。
然后,在my-component 的模板中,我将templ 传递给ngTemplateOutlet,此外,我使用ngOutletContext 传递上下文,其中isVisible 设置为true。
我们应该在屏幕上看到yes!,但似乎从未传递过上下文。
我的角度版本:
"@angular/common": "^2.3.1",
"@angular/compiler": "^2.3.1",
"@angular/core": "^2.3.1",
【问题讨论】:
-
我面临同样的问题。你是对的,上下文永远不会传递。这是因为您作为内容传递给
的模板实际上已绑定到 主机的上下文。我也想让它工作,但我还没有看到任何方法。 -
@AlexanderLeonov 看看我的回答。我找到了。
标签: javascript angular typescript angular2-template angular2-directives