|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {Directive, DoCheck, EmbeddedViewRef, Input, IterableChangeRecord, IterableChanges, IterableDiffer, IterableDiffers, NgIterable, TemplateRef, TrackByFunction, ViewContainerRef} from '@angular/core'; |
| 9 | +import {Directive, DoCheck, EmbeddedViewRef, Input, IterableChangeRecord, IterableChanges, IterableDiffer, IterableDiffers, NgIterable, TemplateRef, TrackByFunction, ViewContainerRef, ɵRuntimeError as RuntimeError} from '@angular/core'; |
| 10 | + |
| 11 | +import {RuntimeErrorCode} from '../errors'; |
| 12 | + |
| 13 | +const NG_DEV_MODE = typeof ngDevMode === 'undefined' || !!ngDevMode; |
10 | 14 |
|
11 | 15 | /** |
12 | 16 | * @publicApi |
@@ -160,7 +164,7 @@ export class NgForOf<T, U extends NgIterable<T> = NgIterable<T>> implements DoCh |
160 | 164 | */ |
161 | 165 | @Input() |
162 | 166 | set ngForTrackBy(fn: TrackByFunction<T>) { |
163 | | - if ((typeof ngDevMode === 'undefined' || ngDevMode) && fn != null && typeof fn !== 'function') { |
| 167 | + if (NG_DEV_MODE && fn != null && typeof fn !== 'function') { |
164 | 168 | // TODO(vicb): use a log service once there is a public one available |
165 | 169 | if (<any>console && <any>console.warn) { |
166 | 170 | console.warn( |
@@ -209,14 +213,18 @@ export class NgForOf<T, U extends NgIterable<T> = NgIterable<T>> implements DoCh |
209 | 213 | // React on ngForOf changes only once all inputs have been initialized |
210 | 214 | const value = this._ngForOf; |
211 | 215 | if (!this._differ && value) { |
212 | | - if (typeof ngDevMode === 'undefined' || ngDevMode) { |
| 216 | + if (NG_DEV_MODE) { |
213 | 217 | try { |
214 | 218 | // CAUTION: this logic is duplicated for production mode below, as the try-catch |
215 | 219 | // is only present in development builds. |
216 | 220 | this._differ = this._differs.find(value).create(this.ngForTrackBy); |
217 | 221 | } catch { |
218 | | - throw new Error(`Cannot find a differ supporting object '${value}' of type '${ |
219 | | - getTypeName(value)}'. NgFor only supports binding to Iterables such as Arrays.`); |
| 222 | + let errorMessage = `Cannot find a differ supporting object '${value}' of type '` + |
| 223 | + `${getTypeName(value)}'. NgFor only supports binding to Iterables, such as Arrays.`; |
| 224 | + if (typeof value === 'object') { |
| 225 | + errorMessage += ' Did you mean to use the keyvalue pipe?'; |
| 226 | + } |
| 227 | + throw new RuntimeError(RuntimeErrorCode.NG_FOR_MISSING_DIFFER, errorMessage); |
220 | 228 | } |
221 | 229 | } else { |
222 | 230 | // CAUTION: this logic is duplicated for development mode above, as the try-catch |
|
0 commit comments