Home > Mobile >  How to set the value of variable for id of HTML-element in HTML-code?
How to set the value of variable for id of HTML-element in HTML-code?

Time:08-17

For example, counter is my variable.

And I wanna make like:

<input id="counter(I mean value of counter)" type="text" mozactionhint="next" name="sometext" />

Or, for example:

<table>
  <tbody>
    <tr *ngIf="currentNode?.parameters.length != 0">
      <td align="center">Type</td>
      <td align="center">Key</td>
      <td align="center">Value</td>
    </tr>
    <tr *ngFor="let item of getParametersWithoutName(); index as i;">
      <td>
        <select>
          <option>One-line</option>
          <option>Multiline</option>
        </select>
      </td>
      <td>< id="i (value of i)" input [ngModel]="item.name" (ngModelChange)="item.name = $event"/></td>
      <td><input [ngModel]="item.value" (ngModelChange)="item.value = $event"/></td>
    </tr>
  </tbody>
</table>

CodePudding user response:

If you want to dynamically set the id of an element in angular you can use bracket notation Like so:

// component.ts

counter = 1;


// template.html

<input [id]="counter"> </input>
  • Related