Skip to content

Commit e460da9

Browse files
gkalpakpetebacondarwin
authored andcommitted
fix($compile): do not overwrite values set in $onInit() for <-bound literals
See angular#15118 for more details. Fixes angular#15118 Closes angular#15123
1 parent ef81003 commit e460da9

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
@@ -3487,18 +3487,21 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
34873487
if (optional && !attrs[attrName]) break;
34883488

34893489
parentGet = $parse(attrs[attrName]);
3490+
var deepWatch = parentGet.literal;
34903491

34913492
var initialValue = destination[scopeName] = parentGet(scope);
34923493
initialChanges[scopeName] = new SimpleChange(_UNINITIALIZED_VALUE, destination[scopeName]);
34933494

34943495
removeWatch = scope.$watch(parentGet, function parentValueWatchAction(newValue, oldValue) {
34953496
if (oldValue === newValue) {
3496-
if (oldValue === initialValue) return;
3497+
if (oldValue === initialValue || (deepWatch && equals(oldValue, initialValue))) {
3498+
return;
3499+
}
34973500
oldValue = initialValue;
34983501
}
34993502
recordChanges(scopeName, newValue, oldValue);
35003503
destination[scopeName] = newValue;
3501-
}, parentGet.literal);
3504+
}, deepWatch);
35023505

35033506
removeWatchCollection.push(removeWatch);
35043507
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)