Snowball
Snowball

Reputation: 1248

AngularJS - How to see what is triggering $scope.$apply()

I have a large project where one method attached to the scope has a console.log in it.

$scope.someFunctionAttachedToView = function() {
    console.log("this method is being triggered");
    return {
       'width': '500px'
    }
}

In this project, I have lots of listeners that listen for browser window size, and mouse clicks etc. and I noticed that sometimes, even if I don't think I'm making an action that would trigger a digest cycle, I see the console.log message above appearing in the browser console intermittently.

I am trying to track down what action could be possibly triggering the digest cycle to apply, as it is not intended behavior.

How would you track this? Is there something I can console.log in the Angular object that would tell me this?

Upvotes: 0

Views: 75

Answers (1)

Martijn Welker
Martijn Welker

Reputation: 5605

If you're working with chrome/firefox you could put a debugger in your code like this:

$scope.someFunctionAttachedToView = function() {
    debugger;   
    console.log("this method is being triggered");
    return {
       'width': '500px'
    }
}

When this function triggers your javascript will be frozen and you can look in the stack trace which function called it. (In chrome you might have to enable async because digests often mess up the call stack)

Upvotes: 0

Related Questions