Skip to content

Commit daabcb5

Browse files
committed
fix($compile): do not overwrite values set in $onInit() for <-bound literals
See angular#15118 for more details. Fixes angular#15118
1 parent d14c7f3 commit daabcb5

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/ng/compile.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -3506,18 +3506,21 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
35063506
if (optional && !attrs[attrName]) break;
35073507

35083508
parentGet = $parse(attrs[attrName]);
3509+
var deepWatch = parentGet.literal;
35093510

35103511
var initialValue = destination[scopeName] = parentGet(scope);
35113512
initialChanges[scopeName] = new SimpleChange(_UNINITIALIZED_VALUE, destination[scopeName]);
35123513

35133514
removeWatch = scope.$watch(parentGet, function parentValueWatchAction(newValue, oldValue) {
35143515
if (oldValue === newValue) {
3515-
if (oldValue === initialValue) return;
3516+
if (oldValue === initialValue || (deepWatch && equals(oldValue, initialValue))) {
3517+
return;
3518+
}
35163519
oldValue = initialValue;
35173520
}
35183521
recordChanges(scopeName, newValue, oldValue);
35193522
destination[scopeName] = newValue;
3520-
}, parentGet.literal);
3523+
}, deepWatch);
35213524

35223525
removeWatchCollection.push(removeWatch);
35233526
break;

test/ng/compileSpec.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -5476,7 +5476,7 @@ describe('$compile', function() {
54765476
expect($rootScope.name).toEqual('outer');
54775477
expect(component.input).toEqual('$onInit');
54785478

5479-
$rootScope.$apply();
5479+
$rootScope.$digest();
54805480

54815481
expect($rootScope.name).toEqual('outer');
54825482
expect(component.input).toEqual('$onInit');
@@ -5489,6 +5489,37 @@ describe('$compile', function() {
54895489
});
54905490
});
54915491

5492+
it('should not update isolate again after $onInit if outer is a literal', function() {
5493+
module('owComponentTest');
5494+
inject(function() {
5495+
$rootScope.name = 'outer';
5496+
compile('<ow-component input="[name]"></ow-component>');
5497+
5498+
expect(component.input).toEqual('$onInit');
5499+
5500+
// No outer change
5501+
$rootScope.$apply('name = "outer"');
5502+
expect(component.input).toEqual('$onInit');
5503+
5504+
// Outer change
5505+
$rootScope.$apply('name = "re-outer"');
5506+
expect(component.input).toEqual(['re-outer']);
5507+
5508+
expect(log).toEqual([
5509+
'constructor',
5510+
[
5511+
'$onChanges',
5512+
jasmine.objectContaining({currentValue: ['outer']})
5513+
],
5514+
'$onInit',
5515+
[
5516+
'$onChanges',
5517+
jasmine.objectContaining({previousValue: ['outer'], currentValue: ['re-outer']})
5518+
]
5519+
]);
5520+
});
5521+
});
5522+
54925523
it('should update isolate again after $onInit if outer has changed (before initial watchAction call)', function() {
54935524
module('owComponentTest');
54945525
inject(function() {

0 commit comments

Comments
 (0)