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

Commit 5b2f361

Browse files
committed
fix($rootScope): Set no context when calling listeners
When calling a listener, do not expose the inner workings with `this`.
1 parent 23395ce commit 5b2f361

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/ng/rootScope.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ function $RootScopeProvider() {
744744
*
745745
*/
746746
$digest: function() {
747-
var watch, value, last,
747+
var watch, value, last, fn,
748748
watchers,
749749
length,
750750
dirty, ttl = TTL,
@@ -798,7 +798,8 @@ function $RootScopeProvider() {
798798
dirty = true;
799799
lastDirtyWatch = watch;
800800
watch.last = watch.eq ? copy(value, null) : value;
801-
watch.fn(value, ((last === initWatchVal) ? value : last), current);
801+
fn = watch.fn;
802+
fn(value, ((last === initWatchVal) ? value : last), current);
802803
if (ttl < 5) {
803804
logIdx = 4 - ttl;
804805
if (!watchLog[logIdx]) watchLog[logIdx] = [];

test/ng/rootScopeSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ describe('Scope', function() {
117117
}));
118118

119119

120+
it('should not expose the `inner working of watch', inject(function($rootScope) {
121+
function Listener() {
122+
expect(this).toBeUndefined();
123+
}
124+
if (msie < 10) return;
125+
$rootScope.$watch('name', Listener);
126+
$rootScope.$digest();
127+
}));
128+
129+
120130
it('should watch and fire on expression change', inject(function($rootScope) {
121131
var spy = jasmine.createSpy();
122132
$rootScope.$watch('name.first', spy);

0 commit comments

Comments
 (0)