Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular not making $http requests immediately

Tags:

angularjs

I have a directive that reacts to an Blur event fired from an input element, and then ultimately results in a call to the Angular $http service to make an appropriate HTTP request.

The $http service is being called correctly and generating the expected promise, however the actual HTTP request does not get made immediately. It is only triggered later after making other kinds of DOM interactions.

Why are the request not being made immediately? And how can I force them to be made immediately?

Angular v1.1.5

like image 934
George Thomas Avatar asked Nov 30 '22 01:11

George Thomas


1 Answers

The reason

I eventually found the answer in the issues on GitHub (https://github.com/angular/angular.js/issues/2442):

v1.1.4... changed the way $http worked so that it was scheduled on nextTick instead of firing immediately. This means that if your $http is scheduled from something that doesn't cause a $digest to fire then your request won't be sent. You can verify that this is the behavior you're seeing by clicking on the page a few times (anywhere) and you should see your request go out.

A Solution

As the post suggested I triggered a $digest cycle, which caused my $http generated requests to be immediately fired.

scope.$apply();
like image 115
George Thomas Avatar answered Dec 11 '22 02:12

George Thomas