From daabcb5e30d69984c439681dc119b8846dad3bcd Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Mon, 12 Sep 2016 16:45:32 +0300 Subject: [PATCH] fix($compile): do not overwrite values set in `$onInit()` for `<`-bound literals See #15118 for more details. Fixes #15118 --- src/ng/compile.js | 7 +++++-- test/ng/compileSpec.js | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 8741dc4d074a..643473f40846 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -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; diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index e4a8ddf4e960..b17ee4a80531 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -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'); @@ -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(''); + + 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() {