Skip to content

Commit a23634d

Browse files
chuckjazIgorMinar
authored andcommitted
fix(common): do not reference deprecated classes in providers (#14523) (#14523)
References to `NgFor` are now an alias for `NgForOf` instead of a derived class. Fixes #14521
1 parent c53621b commit a23634d

File tree

5 files changed

+27
-87
lines changed

5 files changed

+27
-87
lines changed

modules/@angular/common/src/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
export * from './location/index';
1515
export {NgLocaleLocalization, NgLocalization} from './localization';
16-
export {CommonModule, DeprecatedCommonModule} from './common_module';
16+
export {CommonModule} from './common_module';
1717
export {NgClass, NgFor, NgForOf, NgIf, NgPlural, NgPluralCase, NgStyle, NgSwitch, NgSwitchCase, NgSwitchDefault, NgTemplateOutlet, NgComponentOutlet} from './directives/index';
1818
export {AsyncPipe, DatePipe, I18nPluralPipe, I18nSelectPipe, JsonPipe, LowerCasePipe, CurrencyPipe, DecimalPipe, PercentPipe, SlicePipe, UpperCasePipe, TitleCasePipe} from './pipes/index';
1919
export {PLATFORM_BROWSER_ID as ɵPLATFORM_BROWSER_ID, PLATFORM_SERVER_ID as ɵPLATFORM_SERVER_ID, PLATFORM_WORKER_APP_ID as ɵPLATFORM_WORKER_APP_ID, PLATFORM_WORKER_UI_ID as ɵPLATFORM_WORKER_UI_ID, isPlatformBrowser, isPlatformServer, isPlatformWorkerApp, isPlatformWorkerUi} from './platform_id';

modules/@angular/common/src/common_module.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,3 @@ import {COMMON_PIPES} from './pipes/index';
2929
})
3030
export class CommonModule {
3131
}
32-
33-
/**
34-
* A module to contain deprecated directives.
35-
*
36-
* @deprecated
37-
*/
38-
@NgModule({declarations: [COMMON_DEPRECATED_DIRECTIVES], exports: [COMMON_DEPRECATED_DIRECTIVES]})
39-
export class DeprecatedCommonModule {
40-
}

modules/@angular/common/src/directives/ng_for_of.ts

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,8 @@ export class NgForOfRow<T> {
8585
*
8686
* @stable
8787
*/
88-
@Directive({
89-
selector: '[ngFor][ngForOf]',
90-
providers: [{provide: forwardRef(() => NgFor), useExisting: forwardRef(() => NgForOf)}]
91-
})
92-
export class NgForOf<T> implements DoCheck,
93-
OnChanges {
88+
@Directive({selector: '[ngFor][ngForOf]'})
89+
export class NgForOf<T> implements DoCheck, OnChanges {
9490
@Input() ngForOf: NgIterable<T>;
9591
@Input()
9692
set ngForTrackBy(fn: TrackByFunction<T>) {
@@ -191,66 +187,11 @@ class RecordViewTuple<T> {
191187
}
192188

193189
/**
194-
* The `NgFor` directive instantiates a template once per item from an iterable. The context
195-
* for each instantiated template inherits from the outer context with the given loop variable
196-
* set to the current item from the iterable.
197-
*
198-
* ### Local Variables
199-
*
200-
* `NgFor` provides several exported values that can be aliased to local variables:
201-
*
202-
* * `index` will be set to the current loop iteration for each template context.
203-
* * `first` will be set to a boolean value indicating whether the item is the first one in the
204-
* iteration.
205-
* * `last` will be set to a boolean value indicating whether the item is the last one in the
206-
* iteration.
207-
* * `even` will be set to a boolean value indicating whether this item has an even index.
208-
* * `odd` will be set to a boolean value indicating whether this item has an odd index.
209-
*
210-
* ### Change Propagation
211-
*
212-
* When the contents of the iterator changes, `NgFor` makes the corresponding changes to the DOM:
213-
*
214-
* * When an item is added, a new instance of the template is added to the DOM.
215-
* * When an item is removed, its template instance is removed from the DOM.
216-
* * When items are reordered, their respective templates are reordered in the DOM.
217-
* * Otherwise, the DOM element for that item will remain the same.
218-
*
219-
* Angular uses object identity to track insertions and deletions within the iterator and reproduce
220-
* those changes in the DOM. This has important implications for animations and any stateful
221-
* controls (such as `<input>` elements which accept user input) that are present. Inserted rows can
222-
* be animated in, deleted rows can be animated out, and unchanged rows retain any unsaved state
223-
* such as user input.
224-
*
225-
* It is possible for the identities of elements in the iterator to change while the data does not.
226-
* This can happen, for example, if the iterator produced from an RPC to the server, and that
227-
* RPC is re-run. Even if the data hasn't changed, the second response will produce objects with
228-
* different identities, and Angular will tear down the entire DOM and rebuild it (as if all old
229-
* elements were deleted and all new elements inserted). This is an expensive operation and should
230-
* be avoided if possible.
231-
*
232-
* To customize the default tracking algorithm, `NgFor` supports `trackBy` option.
233-
* `trackBy` takes a function which has two arguments: `index` and `item`.
234-
* If `trackBy` is given, Angular tracks changes by the return value of the function.
235-
*
236-
* ### Syntax
237-
*
238-
* - `<li *ngFor="let item of items; let i = index; trackBy: trackByFn">...</li>`
239-
* - `<li template="ngFor let item of items; let i = index; trackBy: trackByFn">...</li>`
240-
*
241-
* With `<template>` element:
242-
*
243-
* ```
244-
* <template ngFor let-item [ngForOf]="items" let-i="index" [ngForTrackBy]="trackByFn">
245-
* <li>...</li>
246-
* </template>
247-
* ```
248-
*
249-
* ### Example
250-
*
251-
* See a [live demo](http://plnkr.co/edit/KVuXxDp0qinGDyo307QW?p=preview) for a more detailed
252-
* example.
253-
*
254-
* @deprecated v4.0.0 - Use `NgForOf<T>` instead.
190+
* @deprecated from v4.0.0 - Use NgForOf<any> instead.
191+
*/
192+
export type NgFor = NgForOf<any>;
193+
194+
/**
195+
* @deprecated from v4.0.0 - Use NgForOf instead.
255196
*/
256-
export class NgFor extends NgForOf<any> {}
197+
export const NgFor = NgForOf;

modules/@angular/common/test/directives/ng_for_spec.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {CommonModule} from '@angular/common';
10-
import {Component} from '@angular/core';
9+
import {CommonModule, NgFor, NgForOf} from '@angular/common';
10+
import {Component, Directive} from '@angular/core';
1111
import {ComponentFixture, TestBed, async} from '@angular/core/testing';
1212
import {By} from '@angular/platform-browser/src/dom/debug/by';
1313
import {expect} from '@angular/platform-browser/testing/matchers';
@@ -29,7 +29,7 @@ export function main() {
2929

3030
beforeEach(() => {
3131
TestBed.configureTestingModule({
32-
declarations: [TestComponent],
32+
declarations: [TestComponent, TestDirective],
3333
imports: [CommonModule],
3434
});
3535
});
@@ -350,6 +350,14 @@ export function main() {
350350
getComponent().items = ['e', 'f', 'h'];
351351
detectChangesAndExpectText('efh');
352352
}));
353+
354+
it('should support injecting `NgFor` and get an instance of `NgForOf`', async(() => {
355+
const template = `<template ngFor [ngForOf]='items' let-item test></template>`;
356+
fixture = createTestComponent(template);
357+
const testDirective = fixture.debugElement.childNodes[0].injector.get(TestDirective);
358+
const ngForOf = fixture.debugElement.childNodes[0].injector.get(NgForOf);
359+
expect(testDirective.ngFor).toBe(ngForOf);
360+
}));
353361
});
354362
});
355363
}
@@ -367,6 +375,11 @@ class TestComponent {
367375
trackByContext(): void { thisArg = this; }
368376
}
369377

378+
@Directive({selector: '[test]'})
379+
class TestDirective {
380+
constructor(public ngFor: NgFor) {}
381+
}
382+
370383
const TEMPLATE = '<div><span *ngFor="let item of items">{{item.toString()}};</span></div>';
371384

372385
function createTestComponent(template: string = TEMPLATE): ComponentFixture<TestComponent> {

tools/public_api_guard/common/typings/common.d.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ export declare class DecimalPipe implements PipeTransform {
3232
transform(value: any, digits?: string): string;
3333
}
3434

35-
/** @deprecated */
36-
export declare class DeprecatedCommonModule {
37-
}
38-
3935
/** @stable */
4036
export declare class HashLocationStrategy extends LocationStrategy {
4137
constructor(_platformLocation: PlatformLocation, _baseHref?: string);
@@ -147,8 +143,7 @@ export declare class NgComponentOutlet implements OnChanges, OnDestroy {
147143
}
148144

149145
/** @deprecated */
150-
export declare class NgFor extends NgForOf<any> {
151-
}
146+
export declare const NgFor: typeof NgForOf;
152147

153148
/** @stable */
154149
export declare class NgForOf<T> implements DoCheck, OnChanges {

0 commit comments

Comments
 (0)