2016-09-06 43 views
2

我有兩個組件,子組件顯示狀態由父級控制。如何讓孩子知道它的父母是否改變了隱藏的屬性?如何讓組件檢測其在angular2中的隱藏狀態?

// Parnet 
@Component(selector: '[component-parent]', 
    templateUrl: 'component-parent.html') 
class ComponentParent { 
    bool enableChild; 
} 

<div> 
    <div component-child [hidden]="enableChild"></div> 
</div> 

// Child 
@Component(selector: '[component-child]', 
    templateUrl: 'component-child.html') 
class ComponentChild { 
    // How child aware not it is not hidden anymore? 
} 

回答

1

組件需要一個@Input()具有匹配名稱以支持[xxx]="..."結合:

@Component(selector: '[component-child]', 
    templateUrl: 'component-child.html') 
class ComponentChild { 
    @Input() 
    set hidden(bool val) { 
    print(val); 
    } 
}