2016-07-29 101 views
0

我有一個組件,它的模板包含'header','content'和'footer'div。 在內容div中,我設置了一個新的自定義指令,它檢查div元素是否有溢出。直到這一步,一切正常。 接下來,我想將我的指令中的數據溢出到主機組件,以便組件知道是否顯示「顯示更多」按鈕和其他配置。Angular 2:指令和主機組件之間的通信

我的問題是如何從我的內部div('內容')指令與其主機組件進行通信?

更新 - 添加代碼示例

tile.component.ts

<div class="tile" > 
<div class="header"> 
    ... 
</div> 
<div class="content" checkOverflow> 
    ... tile content that may be long and contains the chaeckOverflow directive ... 

<div class="footer"> 
    ... 
</div></div> 

checkOverflow.ditective.ts

import { Directive, ElementRef, AfterViewInit } from '@angular/core'; 

@Directive({ 
selector: '[checkOverflow]' 
}) 
export class OverflowDirective implements AfterViewInit { 
    constructor(private el: ElementRef) { 
    } 

    ngAfterViewInit(): void { 

     if (this.el.nativeElement.offsetHeight < this.el.nativeElement.scrollHeight) { 

      console.log('Element has overflow'); 
     } else { 

      console.log('Element has no overflow'); 
     } 
} 

回答