0

I understand that I can add a data-attribute to an element within my component like this:

<div [attr.data-my]="myDataValue">

But how can I add a data-attribute to the component itself? I would expect to declare it like any other @Input, but I can't find any information about it? Does anyone know?

1 Answer 1

2

You can use the following

@HostBinding('attr.data-my')
get someValue(): string {
   return this.myDataValue;
}

You can also add the @HostBinding above an @Input for brevity if it suits your use case - you'll find examples of that too

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.