4
EmbeddedViewRef
のcontext
変数を使用する方法が少し不明です。 Angular 2のchangelogから収集したものから、context
変数は、埋め込みビューでローカル変数を設定するメカニズムとしてsetLocal
とgetLocal
メソッドを置き換えます。この例のplunkerがhereを見つけることができますEmbeddedViewRefのコンテキスト変数の使用方法
import { Directive, TemplateRef, ViewContainerRef } from '@angular/core'
export class FooTemplateContext {
constructor(public bar: string, public baz: string, public qux: string) {}
}
@Directive({
selector: '[foo]'
})
export class Foo {
constructor(viewContainerRef: ViewContainerRef, templateRef: TemplateRef<FooTemplateContext>) {
let context = new FooTemplateContext('bar', 'baz', 'qux');
let view = viewContainerRef.createEmbeddedView(templateRef, context);
}
}
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { Foo } from './foo.directive'
@Component({
selector: 'my-app',
template: `
<div>
<div *foo>
<ul>
<li>{{bar}}</li>
<li>{{baz}}</li>
<li>{{qux}}</li>
</ul>
</div>
</div>
`,
directives: [Foo]
})
export class App {
constructor() {}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
:
setLocal
使用this blog post、見て後、私は一緒に、次の最小限の例をつなぎ合わせています。リストがレンダリングされるとき、各リスト項目は空です。私はcontext
を間違った方法で考えているのか、それとも不適切に設定していますか?もしそうなら、私に知らせてください。
レコードの場合、 '.createEmbeddedView'に渡す' context'オブジェクトはクラスでなければなりません。 JSONオブジェクトにすることはできません – ObjectiveTruth