Angular JS views not updating properly

All we need is an easy explanation of the problem, so here it is.

I have the following action

ng-click in my view -> which inturn calls a jQuery ajax function that displays jQuery Qtip -> In the Qtip popup I have an ng-click on an element -> which performs a $http post and has some callbacks to update $scope values.

So all these things are happening properly. But the updates are not getting reflected in the view as per the changes made in the callback in the final stage.

I have a function for “ng-mousemove” in my view. So whenever I move my mouse, the updates in the view are getting reflected.

What am I doing wrong here? Is the because of transitioning between angular and non-angular? Can anyone help me how to solve this?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

As the docs explains:

$apply() is used to execute an expression in angular from outside of
the angular framework. (For example from browser DOM events,
setTimeout, XHR or third party libraries). Because we are calling into
the angular framework we need to perform proper scope life-cycle of
exception handling, executing watches.

So if you have a jQuery code like

$('#myDiv').on('click', function() {
  // do stuff
  $scope.$apply();
});

Method 2

You have to tell angular Js something has changed with $scope.$apply().

Here is a nice blog on why: http://www.yearofmoo.com/2012/10/more-angularjs-magic-to-supercharge-your-webapp.html#apply-digest-and-phase

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply