Last Updated: February 25, 2016
·
521
· meanjim

a Angular JS dark pattern to avoid.

If you're writing AngularJS you may have seen this error before

Error: $digest already in progress

And to avoid it people have mentioned that you can do the following to check if a $digest is already in progress.

if (!scope.$$phase) {
    // Your Logic
}

But this is not a future-proof way to do it, nor does it guarantee that your logic executes on the next digest loop which is probably what you wanted to happen ( i.e. you changed the scope variable to a different string ). If you want to guarantee that your logic will execute on the next digest loop, do something like this.

$timeout(function() {
    // Your Logic
});