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

fix($parse): allow watching object/array literals #15274

Closed
wants to merge 1 commit into from
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
2 changes: 1 addition & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3522,7 +3522,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
}
recordChanges(scopeName, newValue, oldValue);
destination[scopeName] = newValue;
}, deepWatch);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably makes sense to rename deepWatch now that isn't not used for deep-watching 😃


removeWatchCollection.push(removeWatch);
break;
Expand Down
4 changes: 2 additions & 2 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,7 @@ function $ParseProvider() {
oldInputValueOf = newInputValue && getValueOf(newInputValue);
}
return lastResult;
}, listener, objectEquality, prettyPrintExpression);
}, listener, objectEquality || parsedExpression.literal, prettyPrintExpression);
}

var oldInputValueOfValues = [];
Expand All @@ -1852,7 +1852,7 @@ function $ParseProvider() {
}

return lastResult;
}, listener, objectEquality, prettyPrintExpression);
}, listener, objectEquality || parsedExpression.literal, prettyPrintExpression);
}

function oneTimeWatchDelegate(scope, listener, objectEquality, parsedExpression, prettyPrintExpression) {
Expand Down
23 changes: 23 additions & 0 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3117,6 +3117,29 @@ describe('parser', function() {
scope.$digest();
expect(objB.value).toBe(scope.input);
}));

it('should support watching literals', inject(function($parse) {
var lastVal = NaN;
var callCount = 0;
var listener = function(val) { callCount++; lastVal = val; };

scope.$watch('{val: val}', listener);

scope.$apply('val = 1');
expect(callCount).toBe(1);
expect(lastVal).toEqual({val: 1});

scope.$apply('val = []');
expect(callCount).toBe(2);
expect(lastVal).toEqual({val: []});

scope.$apply('val = []');
expect(callCount).toBe(2);

scope.$apply('val.push(1)');
expect(callCount).toBe(3);
expect(lastVal).toEqual({val: [1]});
}));
});

describe('locals', function() {
Expand Down