Projection

Projection is a very important concept in Angular. It enables developers to build reusable components and make applications more scalable and flexible. To illustrate that, suppose we have a ChildComponent like:

  1. @Component({
  2. selector: 'rio-child',
  3. template: `
  4. <div>
  5. <h4>Child Component</h4>
  6. {{ childContent }}
  7. </div>
  8. `
  9. })
  10. export class ChildComponent {
  11. childContent = "Default content";
  12. }

What should we do if we want to replace {{ childContent }} to any HTML that provided to ChildComponent?One tempting idea is to define an @Input containing the text, but what if you wanted to provide styled HTML, or other components? Trying to handle this with an @Input can get messy quickly, and this is where content projection comes in. Components by default support projection, and you can use the ngContent directive to place the projected content in your template.

So, change ChildComponent to use projection:

app/child/child.component.ts

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'rio-child',
  4. template: `
  5. <div style="border: 1px solid blue; padding: 1rem;">
  6. <h4>Child Component</h4>
  7. <ng-content></ng-content>
  8. </div>
  9. `
  10. })
  11. export class ChildComponent {
  12. }

Then, when we use ChildComponent in the template:

app/app.component.html

  1. ...
  2. <rio-child>
  3. <p>My <i>projected</i> content.</p>
  4. </rio-child>
  5. ...

This is telling Angular, that for any markup that appears between the opening and closing tag of <rio-child>, to place inside of <ng-content></ng-content>.

When doing this, we can have other components, markup, etc projected here and the ChildComponent does not need to know about or care what is being provided.

View Example

But what if we have multiple <ng-content></ng-content> and want to specify the position of the projected content to certain ng-content? For example, for the previous ChildComponent, if we want to format the projected content into an extra header and footer section:

app/child-select.component.html

  1. <div style="...">
  2. <h4>Child Component with Select</h4>
  3. <div style="...">
  4. <ng-content select="header"></ng-content>
  5. </div>
  6. <div style="...">
  7. <ng-content select="section"></ng-content>
  8. </div>
  9. <div style="...">
  10. <ng-content select=".class-select"></ng-content>
  11. </div>
  12. <div style="...">
  13. <ng-content select="footer"></ng-content>
  14. </div>
  15. </div>

Then in the template, we can use directives, say, <header> to specify the position of projected content to the ng-content with select="header":

app/app.component.html

  1. ...
  2. <rio-child-select>
  3. <section>Section Content</section>
  4. <div class="class-select">
  5. div with .class-select
  6. </div>
  7. <footer>Footer Content</footer>
  8. <header>Header Content</header>
  9. </rio-child-select>
  10. ...

Besides using directives, developers can also select a ng-content through css class:

  1. <ng-content select=".class-select"></ng-content>

app/app.component.html

  1. <div class="class-select">
  2. div with .class-select
  3. </div>

View Example

原文: https://angular-2-training-book.rangle.io/handout/components/projection.html