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

fix(common): throw an error if trackBy is not a function #13420

Merged
merged 2 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions modules/@angular/common/src/directives/ng_for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,18 @@ export class NgForRow {
@Directive({selector: '[ngFor][ngForOf]'})
export class NgFor implements DoCheck, OnChanges {
@Input() ngForOf: any;
@Input() ngForTrackBy: TrackByFn;
@Input()
set ngForTrackBy(fn: TrackByFn) {
if (typeof fn !== 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

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

if (fn && typeof fn !== 'function') {

}

Would be consistent with the differ:

  constructor(private _trackByFn?: TrackByFn) {
    this._trackByFn = this._trackByFn || trackByIdentity;
  }

throw new Error(`trackBy must be a function, but received ${JSON.stringify(fn)}`);
}
this._trackByFn = fn;
}

get ngForTrackBy(): TrackByFn { return this._trackByFn; }

private _differ: IterableDiffer = null;
private _trackByFn: TrackByFn;

constructor(
private _viewContainer: ViewContainerRef, private _template: TemplateRef<NgForRow>,
Expand Down Expand Up @@ -119,7 +128,7 @@ export class NgFor implements DoCheck, OnChanges {
}
}

ngDoCheck() {
ngDoCheck(): void {
if (this._differ) {
const changes = this._differ.diff(this.ngForOf);
if (changes) this._applyChanges(changes);
Expand Down
10 changes: 10 additions & 0 deletions modules/@angular/common/test/directives/ng_for_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ export function main() {
}));

describe('track by', () => {
it('should throw if trackBy is not a function', async(() => {
const template =
`<template ngFor let-item [ngForOf]="items" [ngForTrackBy]="item?.id"></template>`;
fixture = createTestComponent(template);

getComponent().items = [{id: 1}, {id: 2}];
expect(() => fixture.detectChanges())
.toThrowError(/trackBy must be a function, but received null/);
}));

it('should set the context to the component instance', async(() => {
const template =
`<template ngFor let-item [ngForOf]="items" [ngForTrackBy]="trackByContext.bind(this)"></template>`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const _chromeNumKeyPadMap = {
* @security Tread carefully! Interacting with the DOM directly is dangerous and
* can introduce XSS risks.
*/
/* tslint:disable:requireParameterType */
/* tslint:disable:requireParameterType no-console */
export class BrowserDomAdapter extends GenericBrowserDomAdapter {
parse(templateHtml: string) { throw new Error('parse not implemented'); }
static makeCurrent() { setRootDomAdapter(new BrowserDomAdapter()); }
Expand All @@ -89,7 +89,6 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {

log(error: string): void {
if (window.console) {
// tslint:disable-next-line:no-console
window.console.log && window.console.log(error);
}
}
Expand Down