2017-05-16 13 views
1

親ディレクティブから子構造ディレクティブにアクセスしようとしています。親構造ディレクティブから子構造ディレクティブにアクセスできない

これは私が子供のディレクティブはアクセスできない理由はわからないんだけど、私は子供

@Directive({ 
    selector: '[appChildStruralDirective]' 
}) 
export class ChildStruralDirective { 
    constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef, private renderer: Renderer) { 
    this.viewContainer.createEmbeddedView(this.templateRef); 
    } 

    ngAfterViewInit(): void { 
    } 
} 

@Directive({ 
    selector: '[appParentStruralDirective]' 
}) 
export class ParentStruralDirective { 
    @ViewChild(ChildStruralDirective) viewChild: ChildStruralDirective; 
    @ContentChild(ChildStruralDirective) contentChild: ChildStruralDirective; 

    constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef, private renderer: Renderer) { 
    this.viewContainer.createEmbeddedView(this.templateRef); 
    } 

    ngAfterViewInit(): void { 
    console.log('ngAfterViewInit:viewChild', this.viewChild); 
    console.log('ngAfterViewInit:contentChild', this.contentChild); 
    } 

    ngAfterContentChecked(): void { 
    console.log('ngAfterContentChecked:viewChild', this.viewChild); 
    console.log('ngAfterContentChecked:contentChild', this.contentChild); 
    } 
} 
@Component({ 
    selector: 'my-app', 
    template: ` 
    <h1 *appParentStruralDirective> 
    <span *appChildStruralDirective>Hello world</span> 
    </h1> 
    `, 
}) 
export class App { 
    constructor() { 
    } 
} 

にアクセスしようとしている方法です。

問題を示すためにplunkrを作成しました。

https://plnkr.co/edit/deQC6cY4oHuNdKbzsfhE?p=preview

これが動作しない理由を私に知らせてください。

PS:私は両方ともViewChild & ContentChild(おそらく候補者)として使用していますが、これはこれらが動的に生成される要素なのでどちらに該当するかはわかりません。

+0

あなたは、サービスの子の構造ディレクティブを登録するためのサービスを注入することができます。 –

+0

それはDIを持っているの目的を破る –

+0

AFAIKそれは動作しません。 githubで問題を作成するhttps://github.com/angular/angular/issues – yurzui

答えて

2

Parent-Childの各組み合わせに対してサービスを作成できます。ここで

はplunkerです:https://plnkr.co/edit/zNf7iZtOQtlsSfYOfq5D?p=preview

@Injectable() 
    export class MyState{ 
    id; 
    child; 
    } 

    @Directive({ 
    selector: '[appChildStruralDirective]' 
    }) 
    export class ChildStruralDirective { 
    random; 

    constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef, private myState : MyState) { 
     this.viewContainer.createEmbeddedView(this.templateRef); 
    this.myState.child = this; 
    this.random = Math.random(); 
    } 

    ngAfterViewInit(): void { 
    } 
    } 

    @Directive({ 
    selector: '[appParentStruralDirective]' 
    }) 
    export class ParentStruralDirective { 
    child; 

    constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef, private renderer: Renderer, 
    private s : MyState) { 
     this.viewContainer.createEmbeddedView(this.templateRef); 
    } 
    } 

    @Component({ 
    selector: 'my-comp', 
    providers: [MyState], 
    template: ` 
    <fieldset style="margin-top:50px"> 
     <button (click)="showMyState()">Show My State</button> 
     <h1 *appParentStruralDirective> 
     <span *appChildStruralDirective>Hello world {{id}}</span> 
     </h1> 
    </fieldset> 
    `, 
    }) 
    export class MyCmp { 
    @Input() id: string; 
    constructor(private myState: MyState) { 
    } 

    ngOnInit(){ 
     this.myState.id=this.id; 
    } 

    showMyState() { 
     console.log(this.myState); 
    } 
    } 

    @Component({ 
    selector: 'my-app', 
    template: ` 
     <my-comp [id]="'one'"></my-comp> 
     <my-comp [id]="'two'"></my-comp> 
    `, 
    }) 
    export class App { 
    constructor() { 
    } 
    } 
+0

plunkrありがとう。 私は、上記のコンポーネントのテンプレート内に 'appParentStruralDirective'のインスタンスを複数持っていれば、親と子のコンボの各インスタンスを参照するサービスごとに複数の一意のキーを持つ必要があると言いました。 (したがって、私の必要条件になります) しかし、これはネイティブ角度ソリューションが利用可能になる前の最初のステップとして役立つはずです。 個別の読み取り専用フィールドを任意のコンポーネントでインライン編集可能にする必要があります。 JIRAのような行に沿ってhttps://hibernate.atlassian.net/browse/HHH-11731 –

関連する問題