Something like that should do the trick.
Parent component :
@Component({
selector: 'parent',
template: '<ng-content></ng-content>'
})
export class ParentComponent {
}
Child component :
@Component({
selector: 'child',
template: ''
})
export class ChildComponent {
constructor(parent: ParentComponent){
// will throw a no provider error if parent is not set
}
}
then you can use your components like you want:
<parent><child></child></parent> <!-- works -->
<child></child> <!-- fails -->
Note that when you inject the parent in the child, the child can actually be the grand-child without throwing an error.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…