0
0
Angularframework~20 mins

Component selector usage in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be rendered by this Angular component usage?

Given the Angular component with selector app-greeting and template <p>Hello, Angular!</p>, what will be the output when used as <app-greeting></app-greeting> in another component's template?

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

@Component({
  selector: 'app-greeting',
  template: '<p>Hello, Angular!</p>'
})
export class GreetingComponent {}
ANo output, component not rendered
B<p>Hello, Angular!</p>
C<app-greeting></app-greeting>
DError: Unknown element 'app-greeting'
Attempts:
2 left
💡 Hint

Think about how Angular replaces component selectors with their templates.

📝 Syntax
intermediate
1:30remaining
Which selector syntax is conventional for an Angular component?

Which of the following selector values is conventional for an Angular component?

A'.app-header'
B'[app-header]'
C'#app-header'
D'app-header'
Attempts:
2 left
💡 Hint

Angular component selectors are usually element selectors.

🔧 Debug
advanced
2:30remaining
Why does this Angular component not render when used as <app-footer></app-footer>?

Given this component code, why does using <app-footer></app-footer> in a template not render anything?

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

@Component({
  selector: '[app-footer]',
  template: '<footer>Footer content</footer>'
})
export class FooterComponent {}
AThe component is missing the @NgModule declaration.
BThe template syntax is invalid and causes a runtime error.
CThe selector is an attribute selector, so <app-footer> won't match it.
DAngular requires selectors to be class selectors for components.
Attempts:
2 left
💡 Hint

Check the selector type and how it matches HTML elements.

state_output
advanced
2:30remaining
What is the output when using a component with a selector that matches multiple elements?

Consider this Angular component with selector div[app-highlight]. What will be rendered when used as below?

<div app-highlight>Text 1</div>
<span app-highlight>Text 2</span>
Angular
import { Component } from '@angular/core';

@Component({
  selector: 'div[app-highlight]',
  template: '<div style="color: red;">Highlighted</div>'
})
export class HighlightComponent {}
A
&lt;div style="color: red;"&gt;Highlighted&lt;/div&gt;
&lt;span app-highlight&gt;Text 2&lt;/span&gt;
B
&lt;div style="color: red;"&gt;Highlighted&lt;/div&gt;
&lt;span style="color: red;"&gt;Text 2&lt;/span&gt;
C
Text 1
&lt;span app-highlight&gt;Text 2&lt;/span&gt;
DError: Multiple elements matched by selector
Attempts:
2 left
💡 Hint

Think about how Angular matches selectors and replaces elements.

🧠 Conceptual
expert
3:00remaining
Why should Angular component selectors avoid common HTML tag names?

Why is it recommended to use custom prefixes like app- in Angular component selectors instead of common HTML tags like header or footer?

ATo prevent conflicts with native HTML elements and ensure Angular renders the component correctly.
BBecause Angular does not allow selectors that match native HTML tags.
CTo improve performance by reducing the number of DOM elements.
DBecause browsers block custom elements without prefixes.
Attempts:
2 left
💡 Hint

Think about how browsers and Angular handle native HTML tags versus custom elements.