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

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

Closes #13388

* refactor(platform-browser): disable no-console rule in DomAdapter
This commit is contained in:
Dzmitry Shylovich
2016-12-21 03:18:24 +03:00
committed by Chuck Jazdzewski
parent b9e979e0a5
commit 44e84d87f9
3 changed files with 22 additions and 4 deletions

View File

@ -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') {
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>,
@ -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);