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

fix($rootScope): Set no context when calling listeners #13909

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
8 changes: 5 additions & 3 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ function $RootScopeProvider() {
*
*/
$digest: function() {
var watch, value, last,
var watch, value, last, fn, get,
watchers,
length,
dirty, ttl = TTL,
Expand Down Expand Up @@ -790,15 +790,17 @@ function $RootScopeProvider() {
// Most common watches are on primitives, in which case we can short
// circuit it with === operator, only when === fails do we use .equals
if (watch) {
if ((value = watch.get(current)) !== (last = watch.last) &&
get = watch.get;
if ((value = get(current)) !== (last = watch.last) &&
!(watch.eq
? equals(value, last)
: (typeof value === 'number' && typeof last === 'number'
&& isNaN(value) && isNaN(last)))) {
dirty = true;
lastDirtyWatch = watch;
watch.last = watch.eq ? copy(value, null) : value;
watch.fn(value, ((last === initWatchVal) ? value : last), current);
fn = watch.fn;
fn(value, ((last === initWatchVal) ? value : last), current);
if (ttl < 5) {
logIdx = 4 - ttl;
if (!watchLog[logIdx]) watchLog[logIdx] = [];
Expand Down
14 changes: 14 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ describe('Scope', function() {
}));


it('should not expose the `inner working of watch', inject(function($rootScope) {
function Getter() {
expect(this).toBeUndefined();
return 'foo';
}
function Listener() {
expect(this).toBeUndefined();
}
if (msie < 10) return;
Copy link
Member

Choose a reason for hiding this comment

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

OOC, what is the problem with IE9 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no "strict mode" in IE9, so this is window

$rootScope.$watch(Getter, Listener);
$rootScope.$digest();
}));


it('should watch and fire on expression change', inject(function($rootScope) {
var spy = jasmine.createSpy();
$rootScope.$watch('name.first', spy);
Expand Down