Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($compile): do not overwrite values set in $onInit() for <-bound literals #15123

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3506,18 +3506,21 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
if (optional && !attrs[attrName]) break;

parentGet = $parse(attrs[attrName]);
var deepWatch = parentGet.literal;

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

removeWatch = scope.$watch(parentGet, function parentValueWatchAction(newValue, oldValue) {
if (oldValue === newValue) {
if (oldValue === initialValue) return;
if (oldValue === initialValue || (deepWatch && equals(oldValue, initialValue))) {
return;
}
oldValue = initialValue;
}
recordChanges(scopeName, newValue, oldValue);
destination[scopeName] = newValue;
}, parentGet.literal);
}, deepWatch);

removeWatchCollection.push(removeWatch);
break;
Expand Down
33 changes: 32 additions & 1 deletion test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5476,7 +5476,7 @@ describe('$compile', function() {
expect($rootScope.name).toEqual('outer');
expect(component.input).toEqual('$onInit');

$rootScope.$apply();
$rootScope.$digest();

expect($rootScope.name).toEqual('outer');
expect(component.input).toEqual('$onInit');
Expand All @@ -5489,6 +5489,37 @@ describe('$compile', function() {
});
});

it('should not update isolate again after $onInit if outer is a literal', function() {
module('owComponentTest');
inject(function() {
$rootScope.name = 'outer';
compile('<ow-component input="[name]"></ow-component>');

expect(component.input).toEqual('$onInit');

// No outer change
$rootScope.$apply('name = "outer"');
expect(component.input).toEqual('$onInit');

// Outer change
$rootScope.$apply('name = "re-outer"');
expect(component.input).toEqual(['re-outer']);

expect(log).toEqual([
'constructor',
[
'$onChanges',
jasmine.objectContaining({currentValue: ['outer']})
],
'$onInit',
[
'$onChanges',
jasmine.objectContaining({previousValue: ['outer'], currentValue: ['re-outer']})
]
]);
});
});

it('should update isolate again after $onInit if outer has changed (before initial watchAction call)', function() {
module('owComponentTest');
inject(function() {
Expand Down