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?
import { Component } from '@angular/core'; @Component({ selector: 'app-greeting', template: '<p>Hello, Angular!</p>' }) export class GreetingComponent {}
Think about how Angular replaces component selectors with their templates.
Angular replaces the app-greeting tag with the component's template content, so the output is the paragraph with 'Hello, Angular!'.
Which of the following selector values is conventional for an Angular component?
Angular component selectors are usually element selectors.
Angular component selectors are typically element selectors like 'app-header'. Class selectors ('.app-header'), ID selectors ('#app-header'), and attribute selectors ('[app-header]') are valid but more commonly used for directives.
<app-footer></app-footer>?Given this component code, why does using <app-footer></app-footer> in a template not render anything?
import { Component } from '@angular/core'; @Component({ selector: '[app-footer]', template: '<footer>Footer content</footer>' }) export class FooterComponent {}
Check the selector type and how it matches HTML elements.
The selector '[app-footer]' means the component matches elements with the attribute 'app-footer'. Using
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>
import { Component } from '@angular/core'; @Component({ selector: 'div[app-highlight]', template: '<div style="color: red;">Highlighted</div>' }) export class HighlightComponent {}
Think about how Angular matches selectors and replaces elements.
The selector 'div[app-highlight]' matches only
Why is it recommended to use custom prefixes like app- in Angular component selectors instead of common HTML tags like header or footer?
Think about how browsers and Angular handle native HTML tags versus custom elements.
Using common HTML tags as selectors can cause conflicts because browsers already recognize those tags natively. Using prefixes like 'app-' creates unique custom elements that Angular can safely replace without conflicts.
Practice
selector property in an Angular component define?Solution
Step 1: Understand the role of selector in Angular components
The selector tells Angular what HTML tag or attribute to look for to insert the component's template.Step 2: Differentiate selector from other properties
CSS styles and class names are unrelated to selector; selector is specifically for placement in HTML.Final Answer:
The HTML tag or attribute used to place the component in templates -> Option AQuick Check:
Selector = HTML tag/attribute [OK]
- Confusing selector with styles or class names
- Thinking selector is the component's class name
- Assuming selector defines module info
Solution
Step 1: Recognize selector types in Angular
Angular selectors can be tag names (like 'my-component'), attribute selectors ('[my-component]'), or class selectors ('.my-component').Step 2: Identify valid selector syntax
The option 'my-component' is a valid tag selector string used in the selector property. The other options represent attribute or class selectors.Final Answer:
my-component -> Option BQuick Check:
Tag selector syntax = tag name string [OK]
- Using selector syntax directly instead of HTML usage
- Confusing attribute selector brackets with HTML tags
- Using camelCase instead of kebab-case for tags
selector: '[appHighlight]', how would you use it in HTML?Solution
Step 1: Understand attribute selector syntax
The selector '[appHighlight]' means the component is used as an attribute on an HTML element.Step 2: Match usage to selector
Usingapplies the component to the div element as an attribute, matching the selector.Final Answer:
<div appHighlight></div> -> Option CQuick Check:
Attribute selector = element with attribute [OK]
- Using tag syntax for attribute selectors
- Using class or id instead of attribute
- Forgetting brackets mean attribute selector
@Component({ selector: 'my-widget', template: 'Hi
' }) export class MyWidgetComponent {} Used in HTML as: <my-widget></my-widget>Solution
Step 1: Check selector syntax
The selector 'my-widget' is valid as a tag selector and matches usage <my-widget></my-widget>.Step 2: Verify component registration
Angular requires components to be declared in an NgModule or standalone to render. Missing declaration causes no rendering.Final Answer:
Component is missing @NgModule declarations -> Option AQuick Check:
Missing declaration = no render [OK]
- Thinking selector must have 'app-' prefix
- Confusing attribute selector syntax with tag selector
- Ignoring module declarations
Solution
Step 1: Understand class selector syntax
To use a component as a CSS class selector, prefix the selector with a dot (.) like '.fancy-button'.Step 2: Apply component in HTML
Use the component by adding the class attribute with the selector name: <button class="fancy-button">.Final Answer:
Use selector '.fancy-button' and apply as <button class="fancy-button"> -> Option DQuick Check:
Class selector = dot prefix + class attribute [OK]
- Using id selector (#) instead of class selector (.)
- Confusing attribute selector brackets with class selector
- Applying class selector as tag or attribute
Ready to prove it?
Pass the test and this pattern moves unknown
