Content projection
<ng-content> lets a component wrap content that its parent passes in. Cards, labels and every UI component are built this way. It works on Lynx, with four rules that come from Lynx's elements.
The Lynx rules
- Projected text must land inside a
<text>. Lynx renders text only inside<text>. If your component takes text, put the<ng-content>inside a<text>. Text projected into a<view>doesn't appear. - A nested
<text>is inline. When the parent passes<text>elements into your<text>, they flow as styled runs, like<span>s. - The host element is a real box.
<app-card>becomes a plain, unstyled<view>in the layout, between the parent and your template. When the component must fill or stretch, put the classes on the host withhost: { class: '…' }. - Each projected element takes a native slot. Lynx has a fixed number of slots for native elements, the element pool. To show hundreds of items, use
<list>, which only creates the rows on screen.
Project into a view
src/app/card.ts
- Pass elements, not bare text, into a component that projects into a
<view>. The parent wraps its words in<text>.
Project text
src/app/card-title.ts
- Bare text works here, because it lands inside the component's
<text>. - Nested
<text>styles part of the text. The second usage renders "Order #1042" on one line, with the number muted.
Fill the parent from the host
src/app/panel.ts
- Put sizing on the host. A
flex-1on an inner view does nothing if the unstyled host around it sizes to its content. - A component with no content needs host sizing most. With nothing inside, its host can collapse to zero size, hiding everything in it.
Named slots
src/app/badge.ts
select works as on the web, including inside a <text>.
Conditional projection
src/app/expandable-label.ts
@if and @for work around projected content, inside a <view> or a <text>.
Content queries
src/app/item-list.ts
contentChild() and contentChildren() find projected elements and components, as on the web. With read: ElementRef, you get the Lynx element, not a DOM node.
Related
- Elements and layout: the
<text>and layout rules - Glossary: element pool
- Content projection on angular.dev
