0
0
Angularframework~20 mins

ng-content for slot-based composition in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Content Projection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does ng-content with select attribute work?
Given this Angular component template:
<ng-content select=".header"></ng-content>
<ng-content select=".body"></ng-content>

And this usage:
<app-panel>
  <div class="header">Title</div>
  <div class="body">Content</div>
  <div class="footer">Footer</div>
</app-panel>

What will be rendered inside app-panel?
AOnly the 'header' div will be rendered; 'body' and 'footer' will be ignored.
BAll three divs ('header', 'body', and 'footer') will be rendered in the order they appear.
CNone of the divs will be rendered because 'footer' does not have a matching select.
DOnly the elements with classes 'header' and 'body' will be rendered in their respective slots; the 'footer' div will not appear.
Attempts:
2 left
💡 Hint
Think about how ng-content with select filters projected content.
📝 Syntax
intermediate
2:00remaining
Identify the correct syntax for multiple ng-content slots
Which of the following Angular component templates correctly defines two named slots using ng-content with attribute selectors?
A
&amp;lt;ng-content name=&amp;quot;header&amp;quot;&amp;gt;&amp;lt;/ng-content&amp;gt;
&amp;lt;ng-content name=&amp;quot;footer&amp;quot;&amp;gt;&amp;lt;/ng-content&amp;gt;
B
&amp;lt;ng-content select=&amp;quot;[slot='header']&amp;quot;&amp;gt;&amp;lt;/ng-content&amp;gt;
&amp;lt;ng-content select=&amp;quot;[slot='footer']&amp;quot;&amp;gt;&amp;lt;/ng-content&amp;gt;
C
&amp;lt;ng-content slot=&amp;quot;header&amp;quot;&amp;gt;&amp;lt;/ng-content&amp;gt;
&amp;lt;ng-content slot=&amp;quot;footer&amp;quot;&amp;gt;&amp;lt;/ng-content&amp;gt;
D
&amp;lt;ng-content select=&amp;quot;.header&amp;quot;&amp;gt;&amp;lt;/ng-content&amp;gt;
&amp;lt;ng-content select=&amp;quot;.footer&amp;quot;&amp;gt;&amp;lt;/ng-content&amp;gt;
Attempts:
2 left
💡 Hint
Angular uses CSS selectors in the select attribute to match projected content.
🔧 Debug
advanced
2:00remaining
Why does projected content not appear in the expected slot?
You have this component template:
<ng-content select=".title"></ng-content>
<ng-content></ng-content>

And you use it like this:
<app-card>
  <h1 class="title">Hello</h1>
  <p>Paragraph</p>
</app-card>

But the h1 appears together with the paragraph in the second slot; the first slot is empty. What is the most likely cause?
AThe <code>h1</code> element has a typo in the class name that does not match '.title'.
BThe <code>ng-content</code> with <code>select</code> must come after the default <code>ng-content</code> to work.
CAngular does not support multiple <code>ng-content</code> elements in one component.
DThe <code>h1</code> element is not a valid child for <code>ng-content</code> projection.
Attempts:
2 left
💡 Hint
Check the class names carefully for exact matches.
state_output
advanced
2:00remaining
What is the rendered output with nested ng-content?
Consider these two components:
// child.component.html
<div class="child">
  <ng-content select=".slot1"></ng-content>
  <ng-content></ng-content>
</div>

// parent.component.html
<app-child>
  <div class="slot1">First</div>
  <div>Second</div>
</app-child>

What will be the final rendered HTML inside app-child?
A&lt;div class=&quot;child&quot;&gt;&lt;div&gt;Second&lt;/div&gt;&lt;div class=&quot;slot1&quot;&gt;First&lt;/div&gt;&lt;/div&gt;
B&lt;div class=&quot;child&quot;&gt;&lt;div&gt;Second&lt;/div&gt;&lt;/div&gt;
C&lt;div class=&quot;child&quot;&gt;&lt;div class=&quot;slot1&quot;&gt;First&lt;/div&gt;&lt;div&gt;Second&lt;/div&gt;&lt;/div&gt;
D&lt;div class=&quot;child&quot;&gt;&lt;div class=&quot;slot1&quot;&gt;First&lt;/div&gt;&lt;/div&gt;
Attempts:
2 left
💡 Hint
Remember the order of ng-content elements and how selectors filter content.
🧠 Conceptual
expert
3:00remaining
Why use ng-content with selectors instead of multiple inputs?
Which is the best explanation for why Angular developers use ng-content with select attributes for slot-based composition instead of passing multiple inputs to a component?
A<p>It allows the parent component to pass complex HTML or components directly, preserving structure and styles, which inputs cannot do.</p>
B<p>It improves performance by avoiding change detection on inputs.</p>
C<p>It simplifies the component code by removing the need for any inputs or outputs.</p>
D<p>It automatically binds data from the parent to the child without any extra code.</p>
Attempts:
2 left
💡 Hint
Think about what inputs can and cannot pass compared to content projection.