22

I use the Angular 2.0 framework and try to create an input component. Also I use Google MDL and it's HTML structure required to have labels to input. Angular gives me an exception:

EXCEPTION: Template parse errors:
Can't bind to 'for' since it isn't a known native property ("s="mdl-textfield__input" type="text" id="{{input_id}}">
        <label class="mdl-textfield__label" [ERROR ->]for="{{input_id}}">{{input_label}}</label>
    </div>"): InputUsernameComponent@2:44

Here is the code:

import {Component} from 'angular2/core';
import {Input} from 'angular2/core';

@Component({
    selector: 'input-username',
    template: `<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
        <input class="mdl-textfield__input" type="text" id="{{input_id}}">
        <label class="mdl-textfield__label" for="{{input_id}}">{{input_label}}</label>
    </div>`
})

export class InputUsernameComponent {
    @Input('input-id') input_id: string;
    @Input('input-label') input_label: string;
}

How can I resolve this issue?

1 Answer 1

52

update

In recent Angular2 versions for should be mapped to htmlFor automatically to avoid this kind of problem.

original

You need attribute binding instead of proper binding

<label class="mdl-textfield__label" attr.for="{{input_id}}">{{input_label}}</label>

or

 <label class="mdl-textfield__label" [attr.for]="input_id">{{input_label}}</label>

or

<label class="mdl-textfield__label" htmlFor="{{input_id}}">{{input_label}}</label>

htmlFor is the property that reflects the for attribute (https://developer.mozilla.org/de/docs/Web/API/HTMLLabelElement)

See also HTML - attributes vs properties

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

14 Comments

@Günter : in RC5 this is not working, is it broken now or is there a new way to do it?
Binding to for should now work. AFAIK they added some mapping. How is it broken? The old way should still work.
no binding to for is still broken but it doesn't give me errors when i use any of your methods it just doesnt work. When I add a for in a label I am expecting that when I click on the label the input is focused. However, when I click it nothing happens
here is a link for the question: stackoverflow.com/questions/39163205/…
@deadManN that's what the first line says ("update"). In older versiond this didn't work.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.