2023-11-06 08:42:56 -08:00
|
|
|
# Component host elements
|
|
|
|
|
|
2025-03-24 16:12:51 +01:00
|
|
|
TIP: This guide assumes you've already read the [Essentials Guide](essentials). Read that first if you're new to Angular.
|
2023-11-06 08:42:56 -08:00
|
|
|
|
|
|
|
|
Angular creates an instance of a component for every HTML element that matches the component's
|
|
|
|
|
selector. The DOM element that matches a component's selector is that component's **host element**.
|
|
|
|
|
The contents of a component's template are rendered inside its host element.
|
|
|
|
|
|
2024-07-26 14:43:29 +02:00
|
|
|
```angular-ts
|
2023-11-06 08:42:56 -08:00
|
|
|
// Component source
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'profile-photo',
|
|
|
|
|
template: `
|
|
|
|
|
<img src="profile-photo.jpg" alt="Your profile photo" />
|
|
|
|
|
`,
|
|
|
|
|
})
|
|
|
|
|
export class ProfilePhoto {}
|
|
|
|
|
```
|
|
|
|
|
|
2024-07-22 17:37:24 +02:00
|
|
|
```angular-html
|
2023-11-06 08:42:56 -08:00
|
|
|
<!-- Using the component -->
|
|
|
|
|
<h3>Your profile photo</h3>
|
|
|
|
|
<profile-photo />
|
|
|
|
|
<button>Upload a new profile photo</button>
|
|
|
|
|
```
|
|
|
|
|
|
2024-07-22 17:37:24 +02:00
|
|
|
```angular-html
|
2023-11-06 08:42:56 -08:00
|
|
|
<!-- Rendered DOM -->
|
|
|
|
|
<h3>Your profile photo</h3>
|
|
|
|
|
<profile-photo>
|
|
|
|
|
<img src="profile-photo.jpg" alt="Your profile photo" />
|
|
|
|
|
</profile-photo>
|
|
|
|
|
<button>Upload a new profile photo</button>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In the above example, `<profile-photo>` is the host element of the `ProfilePhoto` component.
|
|
|
|
|
|
|
|
|
|
## Binding to the host element
|
|
|
|
|
|
|
|
|
|
A component can bind properties, attributes, and events to its host element. This behaves
|
|
|
|
|
identically to bindings on elements inside the component's template, but instead defined with
|
|
|
|
|
the `host` property in the `@Component` decorator:
|
|
|
|
|
|
2024-07-26 14:43:29 +02:00
|
|
|
```angular-ts
|
2023-11-06 08:42:56 -08:00
|
|
|
@Component({
|
|
|
|
|
...,
|
|
|
|
|
host: {
|
|
|
|
|
'role': 'slider',
|
|
|
|
|
'[attr.aria-valuenow]': 'value',
|
2024-08-06 08:51:01 +03:00
|
|
|
'[class.active]': 'isActive()',
|
2023-11-06 08:42:56 -08:00
|
|
|
'[tabIndex]': 'disabled ? -1 : 0',
|
|
|
|
|
'(keydown)': 'updateValue($event)',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
export class CustomSlider {
|
|
|
|
|
value: number = 0;
|
|
|
|
|
disabled: boolean = false;
|
2024-08-06 08:51:01 +03:00
|
|
|
isActive = signal(false);
|
2023-11-06 08:42:56 -08:00
|
|
|
updateValue(event: KeyboardEvent) { /* ... */ }
|
|
|
|
|
|
|
|
|
|
/* ... */
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## The `@HostBinding` and `@HostListener` decorators
|
|
|
|
|
|
|
|
|
|
You can alternatively bind to the host element by applying the `@HostBinding` and `@HostListener`
|
|
|
|
|
decorator to class members.
|
|
|
|
|
|
2025-08-31 15:52:44 +02:00
|
|
|
`@HostBinding` lets you bind host properties and attributes to properties and getters:
|
2023-11-06 08:42:56 -08:00
|
|
|
|
2024-07-26 14:43:29 +02:00
|
|
|
```angular-ts
|
2023-11-06 08:42:56 -08:00
|
|
|
@Component({
|
|
|
|
|
/* ... */
|
|
|
|
|
})
|
|
|
|
|
export class CustomSlider {
|
|
|
|
|
@HostBinding('attr.aria-valuenow')
|
|
|
|
|
value: number = 0;
|
|
|
|
|
|
|
|
|
|
@HostBinding('tabIndex')
|
2025-08-31 15:52:44 +02:00
|
|
|
get tabIndex() {
|
2023-11-06 08:42:56 -08:00
|
|
|
return this.disabled ? -1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ... */
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`@HostListener` lets you bind event listeners to the host element. The decorator accepts an event
|
|
|
|
|
name and an optional array of arguments:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
export class CustomSlider {
|
|
|
|
|
@HostListener('keydown', ['$event'])
|
|
|
|
|
updateValue(event: KeyboardEvent) {
|
|
|
|
|
/* ... */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**Always prefer using the `host` property over `@HostBinding` and `@HostListener`.** These
|
|
|
|
|
decorators exist exclusively for backwards compatibility.
|
|
|
|
|
|
|
|
|
|
## Binding collisions
|
|
|
|
|
|
|
|
|
|
When you use a component in a template, you can add bindings to that component instance's element.
|
|
|
|
|
The component may _also_ define host bindings for the same properties or attributes.
|
|
|
|
|
|
2024-07-26 14:43:29 +02:00
|
|
|
```angular-ts
|
2023-11-06 08:42:56 -08:00
|
|
|
@Component({
|
|
|
|
|
...,
|
|
|
|
|
host: {
|
|
|
|
|
'role': 'presentation',
|
|
|
|
|
'[id]': 'id',
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
export class ProfilePhoto { /* ... */ }
|
|
|
|
|
```
|
|
|
|
|
|
2024-07-22 17:37:24 +02:00
|
|
|
```angular-html
|
2023-11-06 08:42:56 -08:00
|
|
|
<profile-photo role="group" [id]="otherId" />
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In cases like this, the following rules determine which value wins:
|
|
|
|
|
|
|
|
|
|
- If both values are static, the instance binding wins.
|
|
|
|
|
- If one value is static and the other dynamic, the dynamic value wins.
|
|
|
|
|
- If both values are dynamic, the component's host binding wins.
|