テンプレートを渡すコンポーネントがあります。このコンポーネントの内部では、データを表示できるようにコンテキストを渡したいと思います。Angular2のngOutletContextを使用してテンプレートにコンテキストを渡す
今@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
に、私は名前templ
と@ContentChild
ことによってそれのクラスで処理されたテンプレートを渡しています。
その後、my-component
のテンプレートでは、私はngTemplateOutlet
にtempl
を渡していますし、加えて、私はtrue
にisVisible
セットを持っていngOutletContext
を使用してコンテキストを渡しています。
画面にはyes!
が表示されますが、コンテキストは決して通過しないようです。
マイ角度バージョン:
"@angular/common": "^2.3.1",
"@angular/compiler": "^2.3.1",
"@angular/core": "^2.3.1",
の変更ログを検索します。あなたは正しい、文脈は決して通過しない。これは、コンテンツとしてに渡すテンプレートが実際にのホストのコンテキストにバインドされているためです。私はこれもやってみたいですが、私はまだ何も見ません。 –
@AlexanderLeonov私の答えを見てください。私はそれを見つけた。 – Tukkan