Issue
I have a component which I need to dynamically set the id attribute for. This is made up of a some text and another variable, example below.
export class TestComponent {
@Input() public groupId: number = null;
}
The id of this component should be 'fg1' if the groupId variable is 1, 'fg2' if the group is 2 etc.
What is the best way to accomplish this in Angular 15?
Solution
A simple approach in the latest versions of Angular (15+ I believe)
export class TestComponent {
@Input({ transform: (v: number) => v ? `fg${v}` : null })
@HostBinding('attr.id')
groupId: number = null;
}
EDIT : to not change the value, v15 :
export class TestComponent {
@Input()
set groupId(v: number) {
if (v) this.id = `fg${v}`;
}
@HostBinding('attr.id')
id: string = null;
}
Answered By - MGX
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.