Skip to content

Commit 634b3bb

Browse files
pkozlowski-opensourcevicb
authored andcommitted
feat(core): map 'for' attribute to 'htmlFor' property (#10546)
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
1 parent 4595a61 commit 634b3bb

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

modules/@angular/compiler/src/schema/dom_element_schema_registry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ const SCHEMA:
231231

232232
const _ATTR_TO_PROP: {[name: string]: string} = {
233233
'class': 'className',
234+
'for': 'htmlFor',
234235
'formaction': 'formAction',
235236
'innerHtml': 'innerHTML',
236237
'readonly': 'readOnly',

modules/@angular/core/test/linker/integration_spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,19 @@ function declareTests({useJit}: {useJit: boolean}) {
217217
expect(nativeEl).not.toHaveCssClass('initial');
218218
});
219219

220+
it('should consume binding to htmlFor using for alias', () => {
221+
const template = '<label [for]="ctxProp"></label>';
222+
const fixture = TestBed.configureTestingModule({declarations: [MyComp]})
223+
.overrideComponent(MyComp, {set: {template}})
224+
.createComponent(MyComp);
225+
226+
const nativeEl = fixture.debugElement.children[0].nativeElement;
227+
fixture.debugElement.componentInstance.ctxProp = 'foo';
228+
fixture.detectChanges();
229+
230+
expect(nativeEl.htmlFor).toBe('foo');
231+
});
232+
220233
it('should consume directive watch expression change.', () => {
221234
TestBed.configureTestingModule({declarations: [MyComp, MyDir]});
222235
const template = '<span>' +

0 commit comments

Comments
 (0)