访问'B'组件中'A'的视图

时间:2018-01-11 04:59:26

标签: angular

尝试访问类UploadModal中的方法,方法被调用但是 _file

的错误
export class UploadModal {
   @ViewChild(NgFileSelect) _file: NgFileSelect;

  // this method is called
  calledMethod(event, fileType) { 

  const name: string = this._file.uploader._queue[0].name; // getting this._file undefined

}

export class ImageComponent {   // other class in which I need to only use methods in class UploadModal
    callingMethod($event, fileType) {
        let uploadModal = new UploadModal();
        uploadModal.calledMethod($event, fileType);
    }
}

2 个答案:

答案 0 :(得分:0)

您需要在名为<?xml version="1.0"?> <z-Component z-type="ActionBar" z-base="Page" z-namespace="UI.Pages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./../.zebble-schema.xml"> <Stack Style.Margin.Top="10" Style.Margin.Left="5"> <Drawing> <Drawing.Line></Drawing.Line> </Drawing> </Stack> </z-Component> 的生命周期钩子中通过var __drawing_Line1 = new Drawing.Line { Id = "__drawing_Line1" }; 获取孩子。在这个循环中,您的视图已准备好,它可以让孩子。

来自Documentation

  

在调用ngAfterViewInit回调之前设置视图查询。

ViewChild

答案 1 :(得分:0)

试试这个代码段

export class ImageComponent {   
    componentRef: UploadModal;
    constructor(){ this.componentRef = new UploadModal(); }
    callingMethod($event, fileType) {
        this.componentRef.calledMethod($event, fileType);
    }
}