
Previously, using a pipe in an input binding on an ng-template would evaluate the pipe in the context of node that was processed before the template. This caused the retrieval of e.g. ChangeDetectorRef to be incorrect, resulting in one of the following bugs depending on the template's structure: 1. If the template was at the root of a view, the previously processed node would be the component's host node outside of the current view. Accessing that node in the context of the current view results in a crash. 2. For templates not at the root, the ChangeDetectorRef injected into the pipe would correspond with the previously processed node. If that node hosts a component, the ChangeDetectorRef would not correspond with the view that the ng-template is part of. The solution to the above problem is two-fold: 1. Template compilation is adjusted such that the template instruction is emitted before any instructions produced by input bindings, such as pipes. This ensures that pipes are evaluated in the context of the template's container node. 2. A ChangeDetectorRef can be requested for container nodes. Fixes #28587 PR Close #27565
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
import {CommonModule} from '@angular/common';
|
|
import {ChangeDetectorRef, Component, Pipe, PipeTransform} from '@angular/core';
|
|
import {ViewRef} from '@angular/core/src/render3/view_ref';
|
|
import {TestBed} from '@angular/core/testing';
|
|
|
|
describe('di', () => {
|
|
describe('ChangeDetectorRef', () => {
|
|
it('should inject host component ChangeDetectorRef into directives on templates', () => {
|
|
let pipeInstance: MyPipe;
|
|
|
|
@Pipe({name: 'pipe'})
|
|
class MyPipe implements PipeTransform {
|
|
constructor(public cdr: ChangeDetectorRef) { pipeInstance = this; }
|
|
|
|
transform(value: any): any { return value; }
|
|
}
|
|
|
|
@Component({
|
|
selector: 'my-app',
|
|
template: `<div *ngIf="showing | pipe">Visible</div>`,
|
|
})
|
|
class MyApp {
|
|
showing = true;
|
|
|
|
constructor(public cdr: ChangeDetectorRef) {}
|
|
}
|
|
|
|
TestBed.configureTestingModule({declarations: [MyApp, MyPipe], imports: [CommonModule]});
|
|
const fixture = TestBed.createComponent(MyApp);
|
|
fixture.detectChanges();
|
|
expect((pipeInstance !.cdr as ViewRef<MyApp>).context).toBe(fixture.componentInstance);
|
|
});
|
|
});
|
|
});
|