Skip to content

Commit

Permalink
feat(common): better error message when non-template element used in …
Browse files Browse the repository at this point in the history
…NgIf

closes angular#16410
  • Loading branch information
trotyl committed Feb 17, 2018
1 parent f693be3 commit 46d0369
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/common/src/directives/ng_if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive, EmbeddedViewRef, Input, TemplateRef, ViewContainerRef} from '@angular/core';
import {Directive, EmbeddedViewRef, Input, TemplateRef, ViewContainerRef, ɵstringify as stringify} from '@angular/core';


/**
Expand Down Expand Up @@ -118,13 +118,15 @@ export class NgIf {

@Input()
set ngIfThen(templateRef: TemplateRef<NgIfContext>) {
assertTemplate('ngIfThen', templateRef);
this._thenTemplateRef = templateRef;
this._thenViewRef = null; // clear previous view if any.
this._updateView();
}

@Input()
set ngIfElse(templateRef: TemplateRef<NgIfContext>) {
assertTemplate('ngIfElse', templateRef);
this._elseTemplateRef = templateRef;
this._elseViewRef = null; // clear previous view if any.
this._updateView();
Expand Down Expand Up @@ -163,3 +165,10 @@ export class NgIfContext {
public $implicit: any = null;
public ngIf: any = null;
}

function assertTemplate(property: string, templateRef: TemplateRef<any>): void {
const isTemplateRef = templateRef.createEmbeddedView != null;
if (!isTemplateRef) {
throw new Error(`${property} must be a TemplateRef, but received '${stringify(templateRef)}'.`);
}
}
26 changes: 25 additions & 1 deletion packages/common/test/directives/ng_if_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {expect} from '@angular/platform-browser/testing/src/matchers';

{
describe('ngIf directive', () => {
fdescribe('ngIf directive', () => {
let fixture: ComponentFixture<any>;

function getComponent(): TestComponent { return fixture.componentInstance; }
Expand Down Expand Up @@ -217,6 +217,30 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
expect(fixture.nativeElement).toHaveText('false');
}));
});

describe('Type guarding', () => {
it('should throw when then block is not template', async(() => {
const template = '<span *ngIf="booleanCondition; then thenBlock">IGNORE</span>' +
'<div #thenBlock>THEN</div>';

fixture = createTestComponent(template);

expect(() => fixture.detectChanges())
.toThrowError(
`ngIfThen must be a TemplateRef, but received '[object HTMLDivElement]'.`);
}));

it('should throw when else block is not template', async(() => {
const template = '<span *ngIf="booleanCondition; else elseBlock">IGNORE</span>' +
'<div #elseBlock>ELSE</div>';

fixture = createTestComponent(template);

expect(() => fixture.detectChanges())
.toThrowError(
`ngIfElse must be a TemplateRef, but received '[object HTMLDivElement]'.`);
}));
});
});
}

Expand Down

0 comments on commit 46d0369

Please sign in to comment.