Component selectors let you place Angular components inside HTML. They tell Angular where to show your component's content.
Component selector usage in Angular
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent {}The selector is a string that looks like an HTML tag or attribute.
You use the selector as a tag in your HTML to show the component.
app-hello. Use it as <app-hello></app-hello> in HTML.@Component({ selector: 'app-hello', template: '<p>Hello!</p>' })
export class HelloComponent {}[appHighlight]. Add it as an attribute like <div appHighlight></div>.@Component({ selector: '[appHighlight]', template: '<p>Highlighted text</p>' })
export class HighlightComponent {}.app-card. Use it as <div class="app-card"></div>.@Component({ selector: '.app-card', template: '<p>Card content</p>' })
export class CardComponent {}This example shows two components. GreetingComponent uses the selector app-greeting. The AppComponent includes it in its template using the tag <app-greeting></app-greeting>. When the app runs, it shows the greeting message inside the main area.
import { Component } from '@angular/core'; @Component({ selector: 'app-greeting', template: `<h2>Hello, Angular!</h2>` }) export class GreetingComponent {} @Component({ selector: 'app-root', template: `<main> <app-greeting></app-greeting> </main>` }) export class AppComponent {}
Selectors must be unique in your app to avoid conflicts.
Use kebab-case (lowercase with dashes) for selectors to follow Angular style.
Attribute and class selectors are less common but useful for special cases.
Component selectors tell Angular where to place components in HTML.
You can use tag, attribute, or class selectors.
Use selectors as HTML tags or attributes to show your components.