在子组件中获取父组件dom元素

时间:2018-03-11 08:20:32

标签: javascript angular typescript

以下是我的父组件的外观。

在HTML中

    <a href="#" (click)="saveDetails()" #saveDetails>
    <app-tab-product *ngFor="let cat of category"></app-tab-product>//parent component

在打字稿中

//In parent component.    
 saveDetails(){
   if(this.saveDetails.nativeElement.hasAttribute("data-action")== "next"){
      //save from child requested on next click.
    }

}

在我正在使用的子组件typescript

@ViewChild('saveDetails') saveDetails:ElementRef;//child component (tab-product) 但是它显示了未定义的子组件内部。

  this.saveDetails.nativeElement.setAttribute("data-action","next");
  this.saveDetails.nativeElement.click();

子组件内有下一个按钮。如果用户单击子组件内的下一个按钮,我想单击父组件的保存按钮,以便保存子组件的数据。方法saveDetails()位于父组件typescript文件中。

0 个答案:

没有答案