2

I'm trying to have an independent input for each element that I display thanks to *ngFor in Angular2, I try this but it doesn't work :

<li *ngFor="let child of childArray; let i=index"> <input type="checkbox" id="{{child.name}}" style="display:none;">
    <label for="{{child.name}}">
       <h1>{{child.name}}</h1> 
   </label></li>

Of course {{child.name}} is correctly displayed but it seems I can't use it as a variable in tag component. Does someone have an idea to manage it ?

Thanks by advance !

1

1 Answer 1

2

You could try the following:

<li *ngFor="let child of childArray; let i=index">
  <input type="checkbox" [attr.id]="child.name" style="display:none;">
  <label [attr.for]="child.name">
    <h1>{{child.name}}</h1> 
  </label>
</li>

When you say "I can't use it as a variable in tag component", what do you mean? The attribute doesn't contain the value?

Sign up to request clarification or add additional context in comments.

1 Comment

Comment from @slidefizz (to preserve it when his "answer" gets deleted): Thank you for your answer it works with your syntax, by "I can't use it as a variable in tag component" I mean that id="{{child.name}} and for="{{child.name}}" don't work with this syntax, for and id being the tag component (or maybe "tag attribute" is a better meaning) and {{child.name}} the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.