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

$scope.$watch listener expressions can access $newVal & $oldVal #1708

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
7 changes: 6 additions & 1 deletion src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,12 @@ function $RootScopeProvider(){
// in the case user pass string, we need to compile it, do we really need this ?
if (!isFunction(listener)) {
var listenFn = compileToFn(listener || noop, 'listener');
watcher.fn = function(newVal, oldVal, scope) {listenFn(scope);};
watcher.fn = function(newVal, oldVal, scope) {
listenFn(scope, {
$newVal: newVal,
$oldVal: oldVal
});
};
}

if (!array) {
Expand Down
13 changes: 13 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ describe('Scope', function() {
expect(spy).wasCalled();
}));

it('should watch and evaluate a listener expression', inject(function($rootScope) {
var spy = $rootScope.spy = jasmine.createSpy();
$rootScope.$watch('name', 'spy($newVal, $oldVal)');
$rootScope.$digest();
spy.reset();

expect(spy).not.wasCalled();
$rootScope.$digest();
expect(spy).not.wasCalled();
$rootScope.name = 'misko';
$rootScope.$digest();
expect(spy).wasCalledWith('misko', undefined);
}));

it('should delegate exceptions', function() {
module(function($exceptionHandlerProvider) {
Expand Down