1

How to get the parent reference in child directive Angular 2.?

export class ParentComponent<W, T> {
public tags: Array<any>;
public model: T = <T>{};
public widget: W;
protected outputs: Array<string>;
protected twoways: Array<string>;

constructor(protected name: string, protected el: ElementRef, protected cdRef: ChangeDetectorRef) {        
}
}

export function CreateComponent(name: string, componentArgs: {
selector: string,
inputs: Array<string>,
outputs: Array<string>,
directives?: any, 
template: string,
changeDetection?: any,
queries: {
    [key: string]: any
}
}, args): Type<any> {
componentArgs.changeDetection = ChangeDetectionStrategy.OnPush;  

var comp = Component(componentArgs);
return comp.Class({
    extends: ParentComponent,
    constructor: [ElementRef, ChangeDetectorRef, function (el: ElementRef, cdRef: ChangeDetectorRef) {
        this.tags = args.tags;
        this.outputs = componentArgs.outputs;
        this.twoways = args.twoways;
        ParentComponent.call(this, name, el, cdRef);
    }]
})
}


 @Directive({
selector: '[j-template]'     
})


export class JTemplateDirective{   
@Input() widget; 
private childViews : any = [];
constructor(protected el: ElementRef, protected viewContainerRef: ViewContainerRef,
    protected templateRef: TemplateRef<any>, @Host() @Inject(forwardRef(() => EJComponents)) display: EJComponents<any, any>) {

}

ngOnInit(){
}

ngAfterViewInit(){
}
}

In the above j-template directive how can I get the ParentComponet reference and access the properties.?

Karthick
  • 1,241
  • 5
  • 20
  • 43
  • When you define a variable in the parent then pass the variable to the child as an Input, the child can update the variable and the parent will receive the changes. Is that what you need? – Targaryen Oct 21 '16 at 13:52
  • Yes, But I am the component and child creation is dynamic as like in the above code snippet. The CreateComponent() method is generic, whenever I need a custom component, I am able to create it by passing required args to Component class. – Karthick Oct 21 '16 at 14:12

0 Answers0