1

There is this simple component. Some usages require an ID on the input field, others don't. I cannot find out how to leave out the id attribute entirely when the value is falsy. How can this be done?

This is what I have so now:

<div class="form-group row">
  <label [for]="id ? id : ''" class="col-5 col-form-label">{{ label | translate }}:</label>
  <div class="col-7">
      <input type="text" readonly class="form-control-sm form-control-plaintext" [id]="id ? id : ''"
        [value]="value">
  </div>
</div>

But this results in rendering it as

<input type="text" readonly="" class="form-control-sm form-control-plaintext" id="">

For null and undefined the text literally is null and undefined, so that is not working either.

I wish to void having to use *ngIf for this.

Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
  • Does this answer your question? https://stackoverflow.com/questions/36745734/how-to-add-conditional-attribute-in-angular-2 –  Jun 05 '20 at 06:22
  • use null, not '': `id?id:null` :glups:: it's the same that the linked comment – Eliseo Jun 05 '20 at 06:23

0 Answers0