Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular2, unable to assign calculated [for] property on a <label> #7516

Closed
tracker1 opened this issue Mar 9, 2016 · 13 comments · Fixed by #10546
Closed

Angular2, unable to assign calculated [for] property on a <label> #7516

tracker1 opened this issue Mar 9, 2016 · 13 comments · Fixed by #10546

Comments

@tracker1
Copy link

tracker1 commented Mar 9, 2016

[X] bug report
[X] feature request

  • Do you want to request a feature or report a bug?

Bug in existing behavior, feature for proposed solution

I am creating an Angular2 component, in the template, I am wanting to create an <input>, with a calculated [id] property, which works.

I am also wanting to create a <label> with a matching [for] property, so that the label is appropriately bound to the input (clicking will focus said input).

  • What is the current behavior?
EXCEPTION: Template parse errors:
Can't bind to 'for' since it isn't a known native property
  • **If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal
<label [for]="getInputId()">Label</label>
<input [id]="getInputId()" />
  • What is the expected behavior?

The property should be calculated, and the result outputted into the <label> like the [id] property works for <input>;

  • What is the motivation / use case for changing the behavior?

The current behavior doesn't support a necessary feature for HTML output in dealing with label association to form controls.

  • Please tell us about your environment:

Angular version: 2.0.0-beta.7
Browser: all
Language: ES2016+ (Babel)

  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow, gitter, etc)

It would be nice if "unknown" native properties simply run and output the result,as [id] works on the <input> element.

@brandonroberts
Copy link
Contributor

Try using [attr.for] and [attr.id] instead. These are attribute bindings. https://angular.io/docs/ts/latest/guide/template-syntax.html#!#other-bindings

@tracker1
Copy link
Author

tracker1 commented Mar 9, 2016

@brandonroberts thanks... still climbing the hill that is learning Angular2 ... It was just wild that [id] works fine on an <input> but [for] doesn't work on <label> ... perhaps the error message should suggest Did you mean [attr.${unknown_name}]? usage as a hint.

@pkozlowski-opensource
Copy link
Member

@tracker1 see more info in #5435

@tracker1
Copy link
Author

tracker1 commented Mar 9, 2016

@pkozlowski-opensource thanks, I'd searched for it... per your comment, would be nice if there were a property for this, even if it was only a mapping to attr.for, since I can see this being a huge piece of frustration... either way lesson learned... unfortunately, given the relatively common use of for searching for this specific issue was very difficult, and I didn't yield any answer before posting the issue.

In either case, a better error message would probably be the best alternative to adding [for], or another generic handling of unknown properties.

@poke
Copy link

poke commented Mar 14, 2016

It is called property binding, so it binds to properties only, not to attributes. The property is called htmlFor, and that is also what you can bind to:

<label [htmlFor]="getInputId()">Label</label>
<input [id]="getInputId()" />

@tracker1
Copy link
Author

Then the property should be called for... creating unnecessary disconnects from the underlying output only serves to create confusion.

@0x-r4bbit
Copy link
Contributor

Using [attr.for] is the right way to go here. This has been specifically designed for these kind of cases.

I agree with @tracker1 that we should improve the error message here.

@pkozlowski-opensource wdyt?

@poke
Copy link

poke commented Mar 14, 2016

@tracker1 The property name is defined in the HTML standard and has been called that way since the very first formal standardization of the DOM. That’s from 1998. I don’t think Angular should go around renaming standardized properties now.

@tracker1
Copy link
Author

@poke id is an HTML standard as well, for a very long time, and I don't have to use [attr.id] for computed IDs on html components...

Again, I'd be okay with a better error message... in this specific case, for a <label> component, having a property of for that maps to attr.for would probably be a good idea... searching google/stackoverflow for this specific issue was pretty much impossible to determine what the actual problem was.

@zoechi
Copy link
Contributor

zoechi commented Mar 15, 2016

@tracker1 that's because the property id reflects to the attribute id, but the property htmlFor reflects to the attribute for, and all browsers behave the same. If you want this fixed issue a bug report with the w3 spec. Googling for the error messages shows proper references with exact instructions how to cope with the problem.

@pkozlowski-opensource
Copy link
Member

@PascalPrecht I think that we could internally (in the compiler) re-map for -> htmlFor. We do this already in the compiler for few cases (class -> className for example). Technically it is trivial to do but I'm not sure if all the browsers we are targeting are supporting the htmlFor property...

Would anyone be interested in checking cross-browser support for htmlFor property and eventually opening a PR similar to a7a1851 ?

pkozlowski-opensource added a commit to pkozlowski-opensource/angular that referenced this issue Aug 6, 2016
This improves ergonomics a bit by allowing people to write:
`<label [for]="ctxProp"></label>`.
This is similar to the exisiting class -> className mapping.

Closes angular#7516
@vicb
Copy link
Contributor

vicb commented Sep 26, 2016

The error message is now: Can't bind to 'for' since it isn't a known property of 'label'

@vicb vicb closed this as completed Sep 26, 2016
pkozlowski-opensource added a commit to pkozlowski-opensource/angular that referenced this issue Nov 9, 2016
This improves ergonomics a bit by allowing people to write:
`<label [for]="ctxProp"></label>`.
This is similar to the exisiting class -> className mapping.

Closes angular#7516
vicb pushed a commit that referenced this issue Nov 9, 2016
This improves ergonomics a bit by allowing people to write:
`<label [for]="ctxProp"></label>`.
This is similar to the existing class -> className mapping.

Closes #7516
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants