3

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.

Gargoyle
  • 9,590
  • 16
  • 80
  • 145

1 Answers1

4

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

Alex
  • 634
  • 10
  • 29