/TemplateRefAsRenderProps.ts Secret
Last active
October 17, 2017 04:24
-
-
Save isaacplmann/977cba11d25a1402b8de228b18ed02cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We can share code using a regular component with a TemplateRef! | |
@Component({ | |
selector: 'mouse', | |
template: ` | |
<div style="height: 100%" (mousemove)="handleMouseMove($event)"> | |
<ng-container *ngTemplateOutlet="template; context: state" ></ng-container> | |
</div> | |
`, | |
}) | |
export class Mouse { | |
@ContentChild(TemplateRef) template; | |
state = { x: 0, y: 0 } | |
handleMouseMove(event) { | |
this.state = { | |
x: event.clientX, | |
y: event.clientY | |
} | |
} | |
} | |
@Component({ | |
selector: 'my-app', | |
template: ` | |
<div style="height: 100%"> | |
<mouse> | |
<ng-template let-x="x" let-y="y"> | |
// The template gives us the state we need | |
// to render whatever we want here. | |
<h1>The mouse position is ({{x}}, {{y}})</h1> | |
</ng-template> | |
</mouse> | |
</div> | |
`, | |
}) | |
export class AppComponent {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment