Angular2 使用ngcontent 投影来自父组件的html(Projection)

本文介绍了Angular中的投影机制,这是一种让组件更加灵活复用的重要概念。通过使用`<ng-content>`指令,开发者可以在组件模板中放置外部提供的内容,使得组件能够接受任意HTML结构和其他组件作为输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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:

@Component({
    selector: 'rio-child',
    template: `
      <div>
        <h4>Child Component</h4>
        {{ childContent }}
      </div>
    `
})
export class ChildComponent {
  childContent = "Default content";
}

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

import { Component } from '@angular/core';

@Component({
  selector: 'rio-child',
  template: `
    <div style="border: 1px solid blue; padding: 1rem;">
      <h4>Child Component</h4>
      <ng-content></ng-content>
    </div>
  `
})
export class ChildComponent {
}

Then, when we use ChildComponent in the template:

app/app.component.html

...
  <rio-child>
    <p>My <i>projected</i> content.</p>
  </rio-child>
...

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

<div style="...">
  <h4>Child Component with Select</h4>
  <div style="...">
    <ng-content select="header"></ng-content>
  </div>
  <div style="...">
    <ng-content select="section"></ng-content>
  </div>
  <div style="...">
    <ng-content select=".class-select"></ng-content>
  </div>
  <div style="...">
    <ng-content select="footer"></ng-content>
  </div>
</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

...
<rio-child-select>
  <section>Section Content</section>
  <div class="class-select">
    div with .class-select
  </div>
  <footer>Footer Content</footer>
  <header>Header Content</header>
</rio-child-select>
...

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

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

app/app.component.html

<div class="class-select">
  div with .class-select
</div>

View Example

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值