-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add API to inject attributes on the host node (#54604)
Angular has the `@Attribute` decorator that allows for attributes to be injected from the host node, but we don't have an equivalent for the `inject` function. These changes introduce the new `HostAttributeToken` class that can be used to inject attributes similarly to `@Attribute`. It can be used as follows: ```typescript import {HostAttributeToken, inject} from '@angular/core'; class MyDir { someAttr = inject(new HostAttributeToken('some-attr')); } ``` The new API works similarly to `@Attribute` with one key exception: it will throw a DI error when the attribute doesn't exist, instead of returning `null` like `@Attribute`. We made this change to align its behavior closer to other injection tokens. PR Close #54604
- Loading branch information
Showing
6 changed files
with
396 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/*! | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {ɵɵinjectAttribute} from '../render3/instructions/di_attr'; | ||
|
||
/** | ||
* Creates a token that can be used to inject static attributes of the host node. | ||
* | ||
* @usageNotes | ||
* ### Injecting an attribute that is known to exist | ||
* ```typescript | ||
* @Directive() | ||
* class MyDir { | ||
* attr: string = inject(new HostAttributeToken('some-attr')); | ||
* } | ||
* ``` | ||
* | ||
* ### Optionally injecting an attribute | ||
* ```typescript | ||
* @Directive() | ||
* class MyDir { | ||
* attr: string | null = inject(new HostAttributeToken('some-attr'), {optional: true}); | ||
* } | ||
* ``` | ||
* @publicApi | ||
*/ | ||
export class HostAttributeToken { | ||
constructor(private attributeName: string) {} | ||
|
||
/** @internal */ | ||
__NG_ELEMENT_ID__ = () => ɵɵinjectAttribute(this.attributeName); | ||
|
||
toString(): string { | ||
return `HostAttributeToken ${this.attributeName}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.