X Tutup
Skip to content

Commit 345fa52

Browse files
fix(change_detection): convert interpolated null values to empty strings
Fixes #3007 Closes #3271
1 parent 16493e9 commit 345fa52

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

modules/angular2/src/change_detection/change_detection_jit_generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export class ChangeDetectorJITGenerator {
371371
for (var i = 0; i < r.args.length; ++i) {
372372
res += JSON.stringify(r.fixedArgs[i]);
373373
res += " + ";
374-
res += this._localNames[r.args[i]];
374+
res += `${UTIL}.s(${this._localNames[r.args[i]]})`;
375375
res += " + ";
376376
}
377377
res += JSON.stringify(r.fixedArgs[r.args.length]);

modules/angular2/src/change_detection/change_detection_util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,6 @@ export class ChangeDetectionUtil {
151151
}
152152

153153
static isValueBlank(value: any): boolean { return isBlank(value); }
154+
155+
static s(value: any): string { return isPresent(value) ? `${value}` : ''; }
154156
}

modules/angular2/test/change_detection/change_detector_spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,15 @@ export function main() {
302302
expect(val.dispatcher.log).toEqual(['propName=BvalueA']);
303303
});
304304

305+
it('should output empty strings for null values in interpolation', () => {
306+
var val = _createChangeDetector('interpolation', new TestData('value'));
307+
val.changeDetector.hydrate(new TestData(null), null, null, null);
308+
309+
val.changeDetector.detectChanges();
310+
311+
expect(val.dispatcher.log).toEqual(['propName=BA']);
312+
});
313+
305314
it('should escape values in literals that indicate interpolation',
306315
() => { expect(_bindSimpleValue('"$"')).toEqual(['propName=$']); });
307316

modules/angular2/test/core/compiler/integration_spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ export function main() {
9494
});
9595
}));
9696

97+
it('should update text node with a blank string when interpolation evaluates to null',
98+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
99+
tcb.overrideView(MyComp, new viewAnn.View({template: '<div>{{null}}{{ctxProp}}</div>'}))
100+
.createAsync(MyComp)
101+
.then((rootTC) => {
102+
rootTC.componentInstance.ctxProp = null;
103+
104+
rootTC.detectChanges();
105+
expect(rootTC.nativeElement).toHaveText('');
106+
async.done();
107+
});
108+
}));
109+
97110
it('should consume element binding changes',
98111
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
99112
tcb.overrideView(MyComp, new viewAnn.View({template: '<div [id]="ctxProp"></div>'}))

0 commit comments

Comments
 (0)
X Tutup