Skip to content

Commit 8efbcc9

Browse files
committed
fix(compiler): allow to use pipes inside of *ngIf (#10452)
Fixes #9746
1 parent 91c64d2 commit 8efbcc9

File tree

4 files changed

+17
-29
lines changed

4 files changed

+17
-29
lines changed

modules/@angular/compiler-cli/integrationtest/src/features.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export class CompWithProviders {
3838
export class CompWithReferences {
3939
}
4040

41+
@Component({selector: 'cmp-pipes', template: `<div *ngIf>{{test | somePipe}}</div>`})
42+
export class CompUsingPipes {
43+
}
44+
4145
@Component({
4246
selector: 'cmp-custom-els',
4347
template: `

modules/@angular/compiler-cli/integrationtest/src/module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {BrowserModule} from '@angular/platform-browser';
1313
import {AnimateCmp} from './animate';
1414
import {BasicComp} from './basic';
1515
import {CompWithAnalyzeEntryComponentsProvider, CompWithEntryComponents} from './entry_components';
16-
import {CompWithProviders, CompWithReferences, ModuleUsingCustomElements} from './features';
16+
import {CompUsingPipes, CompWithProviders, CompWithReferences, ModuleUsingCustomElements} from './features';
1717
import {CompUsingRootModuleDirectiveAndPipe, SomeDirectiveInRootModule, SomePipeInRootModule, SomeService, someLibModuleWithProviders} from './module_fixtures';
1818
import {ProjectingComp} from './projection';
1919
import {CompWithChildQuery, CompWithDirectiveChild} from './queries';
@@ -23,7 +23,7 @@ import {CompWithChildQuery, CompWithDirectiveChild} from './queries';
2323
SomeDirectiveInRootModule, SomePipeInRootModule, AnimateCmp, BasicComp, CompWithEntryComponents,
2424
CompWithAnalyzeEntryComponentsProvider, ProjectingComp, CompWithChildQuery,
2525
CompWithDirectiveChild, CompUsingRootModuleDirectiveAndPipe, CompWithProviders,
26-
CompWithReferences
26+
CompWithReferences, CompUsingPipes
2727
],
2828
imports: [BrowserModule, FormsModule, someLibModuleWithProviders(), ModuleUsingCustomElements],
2929
providers: [SomeService],

modules/@angular/compiler/src/view_compiler/compile_pipe.ts

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ import * as o from '../output/output_ast';
1515
import {CompileView} from './compile_view';
1616
import {createPureProxy, getPropertyInView, injectFromViewParentInjector} from './util';
1717

18-
class _PurePipeProxy {
19-
constructor(public view: CompileView, public instance: o.ReadPropExpr, public argCount: number) {}
20-
}
21-
2218
export class CompilePipe {
2319
static call(view: CompileView, name: string, args: o.Expression[]): o.Expression {
2420
var compView = view.componentView;
@@ -41,15 +37,10 @@ export class CompilePipe {
4137
}
4238

4339
instance: o.ReadPropExpr;
44-
private _purePipeProxies: _PurePipeProxy[] = [];
40+
private _purePipeProxyCount = 0;
4541

4642
constructor(public view: CompileView, public meta: CompilePipeMetadata) {
4743
this.instance = o.THIS_EXPR.prop(`_pipe_${meta.name}_${view.pipeCount++}`);
48-
}
49-
50-
get pure(): boolean { return this.meta.pure; }
51-
52-
create(): void {
5344
var deps = this.meta.type.diDeps.map((diDep) => {
5445
if (diDep.token.equalsTo(identifierToken(Identifiers.ChangeDetectorRef))) {
5546
return getPropertyInView(o.THIS_EXPR.prop('ref'), this.view, this.view.componentView);
@@ -61,28 +52,22 @@ export class CompilePipe {
6152
this.view.createMethod.addStmt(o.THIS_EXPR.prop(this.instance.name)
6253
.set(o.importExpr(this.meta.type).instantiate(deps))
6354
.toStmt());
64-
this._purePipeProxies.forEach((purePipeProxy) => {
65-
var pipeInstanceSeenFromPureProxy =
66-
getPropertyInView(this.instance, purePipeProxy.view, this.view);
67-
createPureProxy(
68-
pipeInstanceSeenFromPureProxy.prop('transform')
69-
.callMethod(o.BuiltinMethod.bind, [pipeInstanceSeenFromPureProxy]),
70-
purePipeProxy.argCount, purePipeProxy.instance, purePipeProxy.view);
71-
});
7255
}
7356

57+
get pure(): boolean { return this.meta.pure; }
58+
7459
private _call(callingView: CompileView, args: o.Expression[]): o.Expression {
7560
if (this.meta.pure) {
7661
// PurePipeProxies live on the view that called them.
77-
var purePipeProxy = new _PurePipeProxy(
78-
callingView, o.THIS_EXPR.prop(`${this.instance.name}_${this._purePipeProxies.length}`),
79-
args.length);
80-
this._purePipeProxies.push(purePipeProxy);
62+
var purePipeProxyInstance =
63+
o.THIS_EXPR.prop(`${this.instance.name}_${this._purePipeProxyCount++}`);
64+
var pipeInstanceSeenFromPureProxy = getPropertyInView(this.instance, callingView, this.view);
65+
createPureProxy(
66+
pipeInstanceSeenFromPureProxy.prop('transform')
67+
.callMethod(o.BuiltinMethod.bind, [pipeInstanceSeenFromPureProxy]),
68+
args.length, purePipeProxyInstance, callingView);
8169
return o.importExpr(Identifiers.castByValue)
82-
.callFn([
83-
purePipeProxy.instance,
84-
getPropertyInView(this.instance.prop('transform'), callingView, this.view)
85-
])
70+
.callFn([purePipeProxyInstance, pipeInstanceSeenFromPureProxy.prop('transform')])
8671
.callFn(args);
8772
} else {
8873
return getPropertyInView(this.instance, callingView, this.view).callMethod('transform', args);

modules/@angular/compiler/src/view_compiler/compile_view.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ export class CompileView implements NameResolver {
191191
}
192192

193193
afterNodes() {
194-
this.pipes.forEach((pipe) => pipe.create());
195194
this.viewQueries.values().forEach(
196195
(queries) => queries.forEach(
197196
(query) => query.afterChildren(this.createMethod, this.updateViewQueriesMethod)));

0 commit comments

Comments
 (0)