Component Metadata
Angular Component Metadata defines how a component should behave, where its template is located, which styles it uses, and how Angular should identify and configure the component.
What is Component Metadata?
Component metadata is configuration information provided inside the Component decorator. It tells Angular important information about a component.
The metadata is written inside the
@Component({ ... }) decorator.
Component Metadata Flow
Basic Component Metadata
A component commonly contains metadata such as
selector, templateUrl and
styleUrl.
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
templateUrl: './student.component.html',
styleUrl: './student.component.css'
})
export class StudentComponent {
studentName = 'Sam';
}
1. selector
The selector defines the custom HTML element used to
display the component.
selector: 'app-student'
The component can then be used inside another template like this:
<app-student></app-student>
<app-student> is used.
2. templateUrl
The templateUrl property tells Angular where the
component's external HTML template is located.
templateUrl: './student.component.html'
Example template:
<h2>Student Details</h2>
<p>Student Name: {{ studentName }}</p>
Student Details
Student Name: Sam
3. template
Instead of using a separate HTML file, a component can also define
its HTML directly using the template property.
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
template: `
<h2>Student Details</h2>
<p>Student Name: Sam</p>
`
})
export class StudentComponent {
}
Student Details
Student Name: Sam
4. styleUrl
The styleUrl property specifies the CSS file associated
with the component.
styleUrl: './student.component.css'
h2 {
font-size: 26px;
font-weight: 700;
}
p {
font-size: 18px;
}
5. styles
The styles property allows CSS to be written directly
inside the component metadata.
@Component({
selector: 'app-student',
template: `
<h2>Student Details</h2>
<p>Student Name: Sam</p>
`,
styles: [`
h2 {
font-size: 26px;
}
p {
font-size: 18px;
}
`]
})
6. standalone
Modern Angular applications commonly use standalone components.
A standalone component can declare its own dependencies through
the imports metadata property.
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
standalone: true,
template: `
<h2>Student Details</h2>
<p>Student Name: Sam</p>
`
})
export class StudentComponent {
}
7. imports
The imports metadata property is used by standalone
components to make required Angular features or other standalone
components/directives/pipes available to the template.
@Component({
selector: 'app-student',
standalone: true,
imports: [],
template: `
<h2>Student Details</h2>
`
})
Important Metadata Properties
| Property | Purpose |
|---|---|
selector |
Defines how the component is used in HTML. |
template |
Defines an inline HTML template. |
templateUrl |
Points to an external HTML template. |
styles |
Defines inline CSS styles. |
styleUrl |
Points to an external CSS file. |
standalone |
Marks a component as standalone. |
imports |
Provides dependencies used by a standalone component template. |
providers |
Can configure providers available to the component's injector. |
Complete Component Metadata Example
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
standalone: true,
template: `
<div class="student-card">
<h2>Student Details</h2>
<p>Student Name: Sam</p>
</div>
`,
styles: [`
.student-card {
padding: 20px;
border-radius: 8px;
}
h2 {
margin-bottom: 10px;
}
`]
})
export class StudentComponent {
}
Student Details
Student Name: Sam
Metadata Relationship
selector
How the component is placed in HTML.
template
What the component displays.
styles
How the component looks.
standalone
How the component is configured.
Complete Metadata Flow
Component Metadata Best Practices
- Use clear and meaningful component selectors.
- Keep large HTML templates in separate files.
- Keep component-specific styles with the component.
- Use standalone components for modern Angular applications when appropriate.
- Keep metadata simple and easy to understand.
- Use the
importsproperty only for dependencies required by the template.
Summary
Component metadata provides Angular with the configuration required to create and render a component.
Important metadata properties include selector, template, templateUrl, styles, styleUrl, standalone and imports.
Understanding metadata is important because it connects the component class, template, styles and Angular configuration.