Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add attribute to input element [duplicate]

I'm creating a bunch of input elements via an *ngFor statement, and on some of them I want to add an data-bv-integer="true" attribute, and on some I don't. Whether it appears or not is based on a property of the *ngFor item.

Is that possible to do?

I tried to add [attr.data-bv-integer]="false" as a test to see if that would remove the item but it doesn't.

like image 255
Gargoyle Avatar asked Nov 19 '25 20:11

Gargoyle


1 Answers

This is how you can dynamically add attributes to an HTML element with angular:

[<<attribute>>]="<<condition>> ? '<<if true>>' : '<<if false>>'"

In your case it would be:

[attr.data-bv-integer]="shouldShow() ? 'true' : null"

Note: null will remove the attribute

EDIT: Added "attr." thanks @ConnorsFan

like image 143
Alex Avatar answered Nov 22 '25 10:11

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!