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

Commit f927484

Browse files
committed
fix($parse): allow watching object/array literals
1 parent 406c1b0 commit f927484

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/ng/compile.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3508,21 +3508,20 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
35083508
if (optional && !attrs[attrName]) break;
35093509

35103510
parentGet = $parse(attrs[attrName]);
3511-
var deepWatch = parentGet.literal;
35123511

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

35163515
removeWatch = scope.$watch(parentGet, function parentValueWatchAction(newValue, oldValue) {
35173516
if (oldValue === newValue) {
3518-
if (oldValue === initialValue || (deepWatch && equals(oldValue, initialValue))) {
3517+
if (equals(oldValue, initialValue)) {
35193518
return;
35203519
}
35213520
oldValue = initialValue;
35223521
}
35233522
recordChanges(scopeName, newValue, oldValue);
35243523
destination[scopeName] = newValue;
3525-
}, deepWatch);
3524+
});
35263525

35273526
removeWatchCollection.push(removeWatch);
35283527
break;

src/ng/parse.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,7 @@ function $ParseProvider() {
18261826
oldInputValueOf = newInputValue && getValueOf(newInputValue);
18271827
}
18281828
return lastResult;
1829-
}, listener, objectEquality, prettyPrintExpression);
1829+
}, listener, objectEquality || parsedExpression.literal, prettyPrintExpression);
18301830
}
18311831

18321832
var oldInputValueOfValues = [];
@@ -1852,7 +1852,7 @@ function $ParseProvider() {
18521852
}
18531853

18541854
return lastResult;
1855-
}, listener, objectEquality, prettyPrintExpression);
1855+
}, listener, objectEquality || parsedExpression.literal, prettyPrintExpression);
18561856
}
18571857

18581858
function oneTimeWatchDelegate(scope, listener, objectEquality, parsedExpression, prettyPrintExpression) {

test/ng/parseSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -3117,6 +3117,29 @@ describe('parser', function() {
31173117
scope.$digest();
31183118
expect(objB.value).toBe(scope.input);
31193119
}));
3120+
3121+
it('should support watching literals', inject(function($parse) {
3122+
var lastVal = NaN;
3123+
var callCount = 0;
3124+
var listener = function(val) { callCount++; lastVal = val; };
3125+
3126+
scope.$watch('{val: val}', listener);
3127+
3128+
scope.$apply('val = 1');
3129+
expect(callCount).toBe(1);
3130+
expect(lastVal).toEqual({val: 1});
3131+
3132+
scope.$apply('val = []');
3133+
expect(callCount).toBe(2);
3134+
expect(lastVal).toEqual({val: []});
3135+
3136+
scope.$apply('val = []');
3137+
expect(callCount).toBe(2);
3138+
3139+
scope.$apply('val.push(1)');
3140+
expect(callCount).toBe(3);
3141+
expect(lastVal).toEqual({val: [1]});
3142+
}));
31203143
});
31213144

31223145
describe('locals', function() {

0 commit comments

Comments
 (0)