Angular Component Selector
A component selector defines the HTML element, attribute or pattern that Angular uses to identify and display a component.
What is a Component Selector?
The selector is a property inside the @Component decorator. It tells Angular how the component should be used inside another HTML template.
Basic Selector Example
Consider a Student component with the following selector:
import { Component } from '@angular/core';
@Component({
selector: 'app-student'
})
export class StudentComponent {
studentName = 'Sam';
}
The selector is:
1. Element Selector
The most common selector type is an element selector. It allows the component to be used as a custom HTML element.
@Component({
selector: 'app-student'
})
Use the component in an HTML template:
<app-student></app-student>
Result
Angular renders the StudentComponent wherever <app-student> is used.
2. Attribute Selector
A component can also use an attribute selector. In this case, the component is applied through an HTML attribute.
@Component({
selector: '[app-student]'
})
export class StudentComponent {
}
The selector can then be used as an attribute:
<div app-student></div>
Important
Attribute selectors are useful when a component or directive needs to be identified through an HTML attribute.
3. Class Selector
A selector can also be defined using a CSS class pattern.
@Component({
selector: '.student-card'
})
export class StudentComponent {
}
It can then be referenced using the class name:
<div class="student-card"></div>
Selector Types
Element
app-student
Used as a custom HTML element.
Attribute
[app-student]
Used as an HTML attribute.
Class
.student-card
Identifies an element through a class selector.
How Selector Works
Define Selector
Selector is defined in @Component.
Use Selector
Selector is placed in HTML.
Angular Matches
Angular finds the matching element.
Component Rendered
Angular displays the component.
Practical Example
Let's create a Student component and use its selector inside the application.
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
template: `
<h2>Student Details</h2>
<p>Student Name: Sam</p>
`
})
export class StudentComponent {
}
Use the component in the parent template:
<h1>CIIT Student Management</h1>
<app-student></app-student>
Expected Output
CIIT Student Management
Student Details
Student Name: Sam
Selector Naming Convention
Angular projects commonly use a prefix in component selectors. This helps avoid conflicts with existing HTML elements.
app-student
app-header
app-footer
app-dashboard
app-student-list
Selector vs Component Name
Common Selector Mistakes
1. Incorrect selector
Make sure the selector used in the HTML matches the selector defined in the component.
2. Missing component availability
The component must be available in the Angular context where the selector is used.
3. Confusing class and element selectors
Use the correct syntax according to the selector defined in the component.
Summary
A component selector tells Angular how a component is identified and used in an HTML template.
The most common approach is an element selector such as app-student.
Selectors should be meaningful and consistently named throughout the Angular application.