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

See #15118 for more details.

Fixes #15118

Closes #15123
This commit is contained in:
Georgios Kalpakas
2016-09-12 16:45:32 +03:00
parent 52bf2bd11e
commit a1bdffa12f
2 changed files with 37 additions and 3 deletions
+5 -2
View File
@@ -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;
+32 -1
View File
@@ -5524,7 +5524,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');
@@ -5537,6 +5537,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() {