Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/compiler/src/render3/r3_template_transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ class HtmlAstToIvyAst implements html.Visitor {
`Attribute bindings are not supported on ng-container. Use property bindings instead.`,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user wrote [attr.class]="foo" today, they'd get an error telling them to use [class]="foo", which would now also error. Maybe we can report these errors in a way that's a little more clear?

bound.sourceSpan,
);
} else if (
bound.type === BindingType.Class ||
Comment thread
JeanMeche marked this conversation as resolved.
Resolved
(bound.type === BindingType.Property && bound.name === 'class')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be breaking if a directive has a "class" input today?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also check if bound.name === 'style'?

) {
this.reportError(`Class bindings are not supported on ng-container.`, bound.sourceSpan);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions packages/compiler/test/render3/r3_template_transform_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3055,12 +3055,32 @@ describe('R3 template transform', () => {
);
});

it('should report an error for class bindings on ng-container', () => {
const template = `<ng-container [class.foo]="true"></ng-container>`;
const errors = parse(template, {ignoreError: true}).errors;
expect(errors.length).toBe(1);
expect(errors[0].msg).toBe('Class bindings are not supported on ng-container.');
});
it('should report an error for [class] property bindings on ng-container', () => {
const template = `<ng-container [class]="'my-class'"></ng-container>`;
const errors = parse(template, {ignoreError: true}).errors;
expect(errors.length).toBe(1);
expect(errors[0].msg).toBe('Class bindings are not supported on ng-container.');
});

it('should not report an error on non-attr bindings on ng-container', () => {
const template = `<ng-container *ngIf"test" [ngTemplateOutlet]="foo"></ng-container>`;
const errors = parse(template, {ignoreError: true}).errors;
expect(errors.length).toBe(0);
});

it('should report an error for class interpolation on ng-container', () => {
const template = `<ng-container class="{{foo}}"></ng-container>`;
const errors = parse(template, {ignoreError: true}).errors;
expect(errors.length).toBe(1);
expect(errors[0].msg).toBe('Class bindings are not supported on ng-container.');
});

describe('@content blocks', () => {
it('should parse a valid @content block', () => {
expectFromHtml(`
Expand Down
7 changes: 3 additions & 4 deletions packages/core/test/acceptance/host_binding_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ describe('host bindings', () => {
expect(childElement.style.opacity).toEqual('0.5');
});

it('should allow class-bindings to be placed on ng-container elements', () => {
it('should NOT allow class-bindings to be placed on ng-container elements', () => {
@Component({
template: `
<ng-container [class.foo]="true" dir-that-adds-other-classes>...</ng-container>
Expand All @@ -703,9 +703,8 @@ describe('host bindings', () => {

TestBed.configureTestingModule({declarations: [MyApp, DirThatAddsOtherClasses]});
expect(() => {
const fixture = TestBed.createComponent(MyApp);
fixture.detectChanges();
}).not.toThrow();
TestBed.createComponent(MyApp);
}).toThrowError(/Class bindings are not supported on ng-container/);
});
});

Expand Down
Loading