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

Commit ab5c769

Browse files
lgalfasogkalpak
authored andcommitted
fix($rootScope): Set no context when calling helper functions for $watch
When calling a $watch getter or listener, do not expose the inner workings with `this`. Closes: #13909
1 parent 8969050 commit ab5c769

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/ng/rootScope.js

+5-3
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, get,
748748
watchers,
749749
length,
750750
dirty, ttl = TTL,
@@ -790,15 +790,17 @@ function $RootScopeProvider() {
790790
// Most common watches are on primitives, in which case we can short
791791
// circuit it with === operator, only when === fails do we use .equals
792792
if (watch) {
793-
if ((value = watch.get(current)) !== (last = watch.last) &&
793+
get = watch.get;
794+
if ((value = get(current)) !== (last = watch.last) &&
794795
!(watch.eq
795796
? equals(value, last)
796797
: (typeof value === 'number' && typeof last === 'number'
797798
&& isNaN(value) && isNaN(last)))) {
798799
dirty = true;
799800
lastDirtyWatch = watch;
800801
watch.last = watch.eq ? copy(value, null) : value;
801-
watch.fn(value, ((last === initWatchVal) ? value : last), current);
802+
fn = watch.fn;
803+
fn(value, ((last === initWatchVal) ? value : last), current);
802804
if (ttl < 5) {
803805
logIdx = 4 - ttl;
804806
if (!watchLog[logIdx]) watchLog[logIdx] = [];

test/ng/rootScopeSpec.js

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

119119

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

0 commit comments

Comments
 (0)