Reusing Components with Content Projection
Check code at: https://github.com/wghglory/angular2-fundamental
Content Projection
Now in event-detail page, there’s a list of sessions for that event. We want to collapse and expand every session when clicking the session div. Since this feature is quite common, and probably get reused somewhere else, we need to put this function into common folder.
1) In session-list.component.html
1 | <div class="row" *ngFor="let session of sessions"> |
2) create common/collapsible-well.component, and register it in module
Note:
is responsible to show html inside its selector “collapsible-well”: h6+span+span+p in this case
1 | import { Component, Input } from '@angular/core' |
Multiple Slot Content Projection
We also want to add a flame icon next to the session title if any session is hot. If we add this feature into collapsible-well template after <h4 class="well-title">angular2-chapter08 -- Reusing Components with Content Projection</h4>
, this component is not generic.
session-list.component.html:
well-title and well-body attributes can be used as selectors in collapsible-well.component template. Attribute is better than class since it won’t give us any conflict for a css class.
1 | <div class="row" *ngFor="let session of sessions"> |
collapsible-well.component.ts updates template:
Note: ng-content with select is used to display the html. “select” can have id selector, class selector, etc. Attribute selector is best for multiple slot content projection
1 | import { Component, Input } from '@angular/core' |