Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

angular - Can't bind to 'for' since it isn't a known native property angular2

I have to Create a list of checkboxes dynamically so i have used *ngFor to iterate the array of objects everything is working fine till iteration. the problem is occured when i set the value of for attribute in the label tag. angular has throw the error :

Can't bind to 'for' since it isn't a known native property angular2

new error message

Unhandled Promise rejection: Template parse errors: Can't bind to 'for' since it isn't a known property of 'label'.

<div *ngFor="#batch of batch_array">
     <label for="{{batch.id}}"><input type="checkbox" [value]="batch.id" id="{{batch.id}}"    
       (click)="batchSelectedEevent(batch.id)" /> {{batch.batch_name}} 
     </label>
</div>

here is my plnkr showing error : http://plnkr.co/edit/aAQfWvHc7h7IBuYzpItO?p=preview

whats wrong here in my code ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

update

In Angular2 final [for]="xxx" should work fine. They added an alias from for to htmlFor.

original

Angular by default uses property binding but label doesn't have a property for. To tell Angular explicitly to use attribute binding, use instead:

[attr.for]="someField"

or

attr.for="{{someField}}"

instead.

These also work because htmlFor is the property for is reflected to.

[htmlFor]="someField"
htmlFor="{{someField}}"

In Angular2 RC.6 an alias was added so these should now work as well:

[for]="someField" 

or

for="{{someField}}" 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...