Unique ID generation for component instances
Author: byvideCreated Mar 27, 2024Updated Sep 17, 2026
Labelsfeaturearea: corecore: DOM rendering
### Which @angular/* package(s) are relevant/related to the feature request?
core, elements
### Description
In Angular, the reusability of components is a core feature but leads to potential conflicts with HTML standards regarding the uniqueness of id attributes within a document. It particularly impacts accessibility (with aria-labelledby attributes expecting unique id values) and form handling (where label elements use the for attribute to reference input elements by id).
### Proposed solution
Introduce a new field in the component decorator to enable automatic ID uniqueness on a per-instance basis. When this boolean field is set to true, would instruct Angular to automatically prefix id, for, aria-labelledby, and any other attributes that reference IDs, with a unique component instance identifier, ensuring that every instance of a component has uniquely identifiable elements without additional effort from developers.
### Alternatives considered
1. Currently, developers can manually manage unique IDs by incrementing an identifier for each component instance. This approach is simple but prone to human error.
```ts
@Component({
selector: 'app-tracked',
standalone: true,
template: `
Im tracked.
`, }) export class TrackedComponent { private static instance = 0; instance = TrackedComponent.instance++; } ``` 2. Another workaround involves creating custom directives (e.g., [uid] and [ufor]) that communicate through a service to ensure ID uniqueness within the same component instance. Due to the lack of direct references to component instances, this method requires implementing a custom mechanism to track and associate directive instances with their respective component instances. This adds a layer of complexity, as developers must ensure accurate tracking and synchronization across component lifecycles, making it a less than ideal solution.Source: angular/angular