How can I associate labels with form fields outside them in Angular?












0















Let's say I'm creating labels and form fields in a *ngFor loop like this:



app.component.ts



export class AppComponent  {
items = ['aaa', 'bbbbbb', 'ccccccccc']
}


app.component.html



<div class='form'>
<ng-container *ngFor="let item of items">
<label>{{item|uppercase}}:</label>
<input [value]="item"/>
</ng-container>
</div>


(See it on StackBlitz: https://stackblitz.com/edit/angular-ptwq6t)



Is there a way to cleanly associate these "dynamic" labels and inputs with one another? If I do:



<label for="field" >{{item|uppercase}}:</label>
<input id="field" [value]="item"/>


Angular just repeats the for and id attributes verbatim and all labels point to the first input field.



Is there some way to use Angular's component identity, or am I stuck with something like generating a UUID myself, or otherwise guaranteeing uniqueness of the ID myself?



I can't nest the input inside the label because I have to reuse some already implemented CSS that doesn't expect that structure, but would still like the better usability that comes from having a proper label.










share|improve this question



























    0















    Let's say I'm creating labels and form fields in a *ngFor loop like this:



    app.component.ts



    export class AppComponent  {
    items = ['aaa', 'bbbbbb', 'ccccccccc']
    }


    app.component.html



    <div class='form'>
    <ng-container *ngFor="let item of items">
    <label>{{item|uppercase}}:</label>
    <input [value]="item"/>
    </ng-container>
    </div>


    (See it on StackBlitz: https://stackblitz.com/edit/angular-ptwq6t)



    Is there a way to cleanly associate these "dynamic" labels and inputs with one another? If I do:



    <label for="field" >{{item|uppercase}}:</label>
    <input id="field" [value]="item"/>


    Angular just repeats the for and id attributes verbatim and all labels point to the first input field.



    Is there some way to use Angular's component identity, or am I stuck with something like generating a UUID myself, or otherwise guaranteeing uniqueness of the ID myself?



    I can't nest the input inside the label because I have to reuse some already implemented CSS that doesn't expect that structure, but would still like the better usability that comes from having a proper label.










    share|improve this question

























      0












      0








      0








      Let's say I'm creating labels and form fields in a *ngFor loop like this:



      app.component.ts



      export class AppComponent  {
      items = ['aaa', 'bbbbbb', 'ccccccccc']
      }


      app.component.html



      <div class='form'>
      <ng-container *ngFor="let item of items">
      <label>{{item|uppercase}}:</label>
      <input [value]="item"/>
      </ng-container>
      </div>


      (See it on StackBlitz: https://stackblitz.com/edit/angular-ptwq6t)



      Is there a way to cleanly associate these "dynamic" labels and inputs with one another? If I do:



      <label for="field" >{{item|uppercase}}:</label>
      <input id="field" [value]="item"/>


      Angular just repeats the for and id attributes verbatim and all labels point to the first input field.



      Is there some way to use Angular's component identity, or am I stuck with something like generating a UUID myself, or otherwise guaranteeing uniqueness of the ID myself?



      I can't nest the input inside the label because I have to reuse some already implemented CSS that doesn't expect that structure, but would still like the better usability that comes from having a proper label.










      share|improve this question














      Let's say I'm creating labels and form fields in a *ngFor loop like this:



      app.component.ts



      export class AppComponent  {
      items = ['aaa', 'bbbbbb', 'ccccccccc']
      }


      app.component.html



      <div class='form'>
      <ng-container *ngFor="let item of items">
      <label>{{item|uppercase}}:</label>
      <input [value]="item"/>
      </ng-container>
      </div>


      (See it on StackBlitz: https://stackblitz.com/edit/angular-ptwq6t)



      Is there a way to cleanly associate these "dynamic" labels and inputs with one another? If I do:



      <label for="field" >{{item|uppercase}}:</label>
      <input id="field" [value]="item"/>


      Angular just repeats the for and id attributes verbatim and all labels point to the first input field.



      Is there some way to use Angular's component identity, or am I stuck with something like generating a UUID myself, or otherwise guaranteeing uniqueness of the ID myself?



      I can't nest the input inside the label because I have to reuse some already implemented CSS that doesn't expect that structure, but would still like the better usability that comes from having a proper label.







      javascript angular forms typescript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 6:48









      millimoosemillimoose

      32.3k762104




      32.3k762104
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Given that the items are unique, you could surely do this:



          <label [for]="item" >{{item|uppercase}}:</label>
          <input [id]="item" [value]="item"/>


          That way, each id and for would be unique and label would work as required.



          Here is the demo.



          If you anyway need to generate the unique IDs, take a look at shortid






          share|improve this answer
























          • I mean in my dummy example they are but generally I can't guarantee this and components should be self-contained.

            – millimoose
            Nov 21 '18 at 7:04











          • shortid looks neat though

            – millimoose
            Nov 21 '18 at 7:05











          • Yeah, Because the items were being used as labels themselves, I assumed that they would be unique. Duplicate label in a form does not make sense. About shortid, definitely, I have seen people in react community using it quite extensively

            – Anand Undavia
            Nov 21 '18 at 7:08






          • 1





            I’ll probably go nuts and use it to make our forms submittable with Enter too like god intended

            – millimoose
            Nov 21 '18 at 7:45



















          0














          you can try:



           <div class='form'>
          <ng-container *ngFor="let item of items">
          <label for="{{item}} + 'field'" >{{item|uppercase}}:</label>
          <input id="{{item}} + 'field'" [value]="item"/>
          </ng-container>
          </div>


          or use the ngfor index if your items are not unique:



          <div class='form'>
          <ng-container *ngFor="let item of items; let i = index">
          <label for="{{i}} + 'field'" >{{item|uppercase}}:</label>
          <input id="{{i}} + 'field'" [value]="item"/>
          </ng-container>
          </div>


          DEMO






          share|improve this answer


























          • did you check my answer? what is the problem?

            – Fateme Fazli
            Nov 21 '18 at 7:09











          • If I use the component doing this twice on a page the IDs ‘0field’, ‘1field’ will repeat

            – millimoose
            Nov 21 '18 at 7:44











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406626%2fhow-can-i-associate-labels-with-form-fields-outside-them-in-angular%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Given that the items are unique, you could surely do this:



          <label [for]="item" >{{item|uppercase}}:</label>
          <input [id]="item" [value]="item"/>


          That way, each id and for would be unique and label would work as required.



          Here is the demo.



          If you anyway need to generate the unique IDs, take a look at shortid






          share|improve this answer
























          • I mean in my dummy example they are but generally I can't guarantee this and components should be self-contained.

            – millimoose
            Nov 21 '18 at 7:04











          • shortid looks neat though

            – millimoose
            Nov 21 '18 at 7:05











          • Yeah, Because the items were being used as labels themselves, I assumed that they would be unique. Duplicate label in a form does not make sense. About shortid, definitely, I have seen people in react community using it quite extensively

            – Anand Undavia
            Nov 21 '18 at 7:08






          • 1





            I’ll probably go nuts and use it to make our forms submittable with Enter too like god intended

            – millimoose
            Nov 21 '18 at 7:45
















          1














          Given that the items are unique, you could surely do this:



          <label [for]="item" >{{item|uppercase}}:</label>
          <input [id]="item" [value]="item"/>


          That way, each id and for would be unique and label would work as required.



          Here is the demo.



          If you anyway need to generate the unique IDs, take a look at shortid






          share|improve this answer
























          • I mean in my dummy example they are but generally I can't guarantee this and components should be self-contained.

            – millimoose
            Nov 21 '18 at 7:04











          • shortid looks neat though

            – millimoose
            Nov 21 '18 at 7:05











          • Yeah, Because the items were being used as labels themselves, I assumed that they would be unique. Duplicate label in a form does not make sense. About shortid, definitely, I have seen people in react community using it quite extensively

            – Anand Undavia
            Nov 21 '18 at 7:08






          • 1





            I’ll probably go nuts and use it to make our forms submittable with Enter too like god intended

            – millimoose
            Nov 21 '18 at 7:45














          1












          1








          1







          Given that the items are unique, you could surely do this:



          <label [for]="item" >{{item|uppercase}}:</label>
          <input [id]="item" [value]="item"/>


          That way, each id and for would be unique and label would work as required.



          Here is the demo.



          If you anyway need to generate the unique IDs, take a look at shortid






          share|improve this answer













          Given that the items are unique, you could surely do this:



          <label [for]="item" >{{item|uppercase}}:</label>
          <input [id]="item" [value]="item"/>


          That way, each id and for would be unique and label would work as required.



          Here is the demo.



          If you anyway need to generate the unique IDs, take a look at shortid







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 6:56









          Anand UndaviaAnand Undavia

          1,7261923




          1,7261923













          • I mean in my dummy example they are but generally I can't guarantee this and components should be self-contained.

            – millimoose
            Nov 21 '18 at 7:04











          • shortid looks neat though

            – millimoose
            Nov 21 '18 at 7:05











          • Yeah, Because the items were being used as labels themselves, I assumed that they would be unique. Duplicate label in a form does not make sense. About shortid, definitely, I have seen people in react community using it quite extensively

            – Anand Undavia
            Nov 21 '18 at 7:08






          • 1





            I’ll probably go nuts and use it to make our forms submittable with Enter too like god intended

            – millimoose
            Nov 21 '18 at 7:45



















          • I mean in my dummy example they are but generally I can't guarantee this and components should be self-contained.

            – millimoose
            Nov 21 '18 at 7:04











          • shortid looks neat though

            – millimoose
            Nov 21 '18 at 7:05











          • Yeah, Because the items were being used as labels themselves, I assumed that they would be unique. Duplicate label in a form does not make sense. About shortid, definitely, I have seen people in react community using it quite extensively

            – Anand Undavia
            Nov 21 '18 at 7:08






          • 1





            I’ll probably go nuts and use it to make our forms submittable with Enter too like god intended

            – millimoose
            Nov 21 '18 at 7:45

















          I mean in my dummy example they are but generally I can't guarantee this and components should be self-contained.

          – millimoose
          Nov 21 '18 at 7:04





          I mean in my dummy example they are but generally I can't guarantee this and components should be self-contained.

          – millimoose
          Nov 21 '18 at 7:04













          shortid looks neat though

          – millimoose
          Nov 21 '18 at 7:05





          shortid looks neat though

          – millimoose
          Nov 21 '18 at 7:05













          Yeah, Because the items were being used as labels themselves, I assumed that they would be unique. Duplicate label in a form does not make sense. About shortid, definitely, I have seen people in react community using it quite extensively

          – Anand Undavia
          Nov 21 '18 at 7:08





          Yeah, Because the items were being used as labels themselves, I assumed that they would be unique. Duplicate label in a form does not make sense. About shortid, definitely, I have seen people in react community using it quite extensively

          – Anand Undavia
          Nov 21 '18 at 7:08




          1




          1





          I’ll probably go nuts and use it to make our forms submittable with Enter too like god intended

          – millimoose
          Nov 21 '18 at 7:45





          I’ll probably go nuts and use it to make our forms submittable with Enter too like god intended

          – millimoose
          Nov 21 '18 at 7:45













          0














          you can try:



           <div class='form'>
          <ng-container *ngFor="let item of items">
          <label for="{{item}} + 'field'" >{{item|uppercase}}:</label>
          <input id="{{item}} + 'field'" [value]="item"/>
          </ng-container>
          </div>


          or use the ngfor index if your items are not unique:



          <div class='form'>
          <ng-container *ngFor="let item of items; let i = index">
          <label for="{{i}} + 'field'" >{{item|uppercase}}:</label>
          <input id="{{i}} + 'field'" [value]="item"/>
          </ng-container>
          </div>


          DEMO






          share|improve this answer


























          • did you check my answer? what is the problem?

            – Fateme Fazli
            Nov 21 '18 at 7:09











          • If I use the component doing this twice on a page the IDs ‘0field’, ‘1field’ will repeat

            – millimoose
            Nov 21 '18 at 7:44
















          0














          you can try:



           <div class='form'>
          <ng-container *ngFor="let item of items">
          <label for="{{item}} + 'field'" >{{item|uppercase}}:</label>
          <input id="{{item}} + 'field'" [value]="item"/>
          </ng-container>
          </div>


          or use the ngfor index if your items are not unique:



          <div class='form'>
          <ng-container *ngFor="let item of items; let i = index">
          <label for="{{i}} + 'field'" >{{item|uppercase}}:</label>
          <input id="{{i}} + 'field'" [value]="item"/>
          </ng-container>
          </div>


          DEMO






          share|improve this answer


























          • did you check my answer? what is the problem?

            – Fateme Fazli
            Nov 21 '18 at 7:09











          • If I use the component doing this twice on a page the IDs ‘0field’, ‘1field’ will repeat

            – millimoose
            Nov 21 '18 at 7:44














          0












          0








          0







          you can try:



           <div class='form'>
          <ng-container *ngFor="let item of items">
          <label for="{{item}} + 'field'" >{{item|uppercase}}:</label>
          <input id="{{item}} + 'field'" [value]="item"/>
          </ng-container>
          </div>


          or use the ngfor index if your items are not unique:



          <div class='form'>
          <ng-container *ngFor="let item of items; let i = index">
          <label for="{{i}} + 'field'" >{{item|uppercase}}:</label>
          <input id="{{i}} + 'field'" [value]="item"/>
          </ng-container>
          </div>


          DEMO






          share|improve this answer















          you can try:



           <div class='form'>
          <ng-container *ngFor="let item of items">
          <label for="{{item}} + 'field'" >{{item|uppercase}}:</label>
          <input id="{{item}} + 'field'" [value]="item"/>
          </ng-container>
          </div>


          or use the ngfor index if your items are not unique:



          <div class='form'>
          <ng-container *ngFor="let item of items; let i = index">
          <label for="{{i}} + 'field'" >{{item|uppercase}}:</label>
          <input id="{{i}} + 'field'" [value]="item"/>
          </ng-container>
          </div>


          DEMO







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 7:01

























          answered Nov 21 '18 at 6:55









          Fateme FazliFateme Fazli

          4,5171827




          4,5171827













          • did you check my answer? what is the problem?

            – Fateme Fazli
            Nov 21 '18 at 7:09











          • If I use the component doing this twice on a page the IDs ‘0field’, ‘1field’ will repeat

            – millimoose
            Nov 21 '18 at 7:44



















          • did you check my answer? what is the problem?

            – Fateme Fazli
            Nov 21 '18 at 7:09











          • If I use the component doing this twice on a page the IDs ‘0field’, ‘1field’ will repeat

            – millimoose
            Nov 21 '18 at 7:44

















          did you check my answer? what is the problem?

          – Fateme Fazli
          Nov 21 '18 at 7:09





          did you check my answer? what is the problem?

          – Fateme Fazli
          Nov 21 '18 at 7:09













          If I use the component doing this twice on a page the IDs ‘0field’, ‘1field’ will repeat

          – millimoose
          Nov 21 '18 at 7:44





          If I use the component doing this twice on a page the IDs ‘0field’, ‘1field’ will repeat

          – millimoose
          Nov 21 '18 at 7:44


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406626%2fhow-can-i-associate-labels-with-form-fields-outside-them-in-angular%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith