How to skips model dirty checking with $http? - angularjs

You can skip model dirty checking in $timeout service by setting the third parameter called invokeApply to false (see documentation).
Is it possible to achieve the same result with the $http service?
I need to call a legacy third-party restful webservice several times, but must reduce the amount of refreshes on the UI...

No, but you can use jQuery.ajax or any other ajax library to make http requests out of the digest loop.
In fact, $http will not send any request outside the digest loop. This is from the angular comments:
The $http service will not actually send the request until the next $digest() is
executed. Normally this is not an issue, since almost all the time your call to $http will
be from within a $apply() block.
If you are calling $http from outside Angular, then you should wrap it in a call to
$apply to cause a $digest to occur and also to handle errors in the block correctly.

Related

How to know when AngularJS digest cycle ends without using $timeout?

This is a general question about AngularJS.
AngularJS does not raise an event to notify when the digest cycle has ended. AngularJS suggests using $timeout as a solution to queue your work to be run after the current digest cycle (also waits for DOM rendering to be completed by the browser).
Does anyone know how to know if $scope.$apply() and $scope.$digest() have ended without using $timeout?
You can use $scope.$digest(), before your business logic function or $scope.$apply(function(){/*your business*/}) but this method like timeout function.

Angularjs Promise.all not updating scope, while $q.all does

I am using Angularjs 1.3.7 and just discovered that Promise.all does not update angularjs view after a successful response, while $q.all does. Is this later changed since Promises are included in native javascript or what is the reason behind this?
As IAmDranged mentions in a comment:
Probably yeah, because the Promise API is not integrated with the angular mechanism - meaning essentially that it doesn't trigger a digest cycle at key point of the lifecycle of the promises. Try adding a $scope.$apply() at the end of the Promise.all() callback function to trigger a digest cycle manually.
It was confirmed that Promise.all does not trigger a digest cycle.

Angular JS $http promise object -- How does it work?

All,
I'm a beginner to Angular framework and have been reading $http service. I thought I understood Promise object in Angular JS until the below questions popped up my mind. Can you please help me understand?
– When I make a REST call from Angular, Angular work not make the rest call before it executes the other steps in the js from where the REST API is invoked. This is because it’s single threaded. Okay I get this. So when does it execute the REST call? May be after all the instructions in the current JS are done? If yes,
Why do we even call it asynchronous?
Secondly, during the time it executes the REST API (let’s say the REST API results in an output not before 2 seconds), right after it calls REST API would Angular just wait for the 2 seconds without doing anything?
If $http service executes the REST API asynchronously as soon as it sees it,
Who spawns the second thread to execute the REST API? The framework? If so then are they making Javascript no longer be uni–threaded?
Secondly when the service returns while the main thread hasn’t even completed executing all the lines in my js, does Angular/jQuery goes and executes the instructions coded in the .then function, leaving the instructions in the main thread waiting?
Thanks much for your help!
Prem
When I make a REST call from Angular, Angular work not make the rest call before it executes the other steps in the js from where the REST API is invoked
No. That is incorrect. It sends the request immediately. But it doesn't block until the response comes back, because that could take a whole lot of time and completely freezes the application. So instead, it registers a callback, and this callback will be called, later, when the response is available. That's the principle of asynchronism.
Who spawns the second thread to execute the REST API
There is no second thread.
The easiest way to look at it is to consider that an HTTP response is an event, just like a click or a keypress. All the unique thread does is to wait for the next event and react to it, in a loop.
First of all, it wont block the execution, it sends the request immediately. Basically it follow the promise pattern which uses the concept of callback function. So whenever API returns the response it calls the callback function and resume the process.
There is no concept like second thread. When HTTP return the response the execution doesn't stop. it just call its callback method and after that excute the remaining line of code

How do we cancel the pending $http requests when the route changes?

We can abort the $timeout and $interval by using '$destroy' when the route changes. How can we abort the pending $http requests like $interval and $timeout?
Check out this article, it has some valuable information. Throughout the research I made, there is no magical solution unfortunately. You'll have to get your hands dirty and code a bit for some results.

Angular ngmock httpBackend - ignore all but one request?

I am new to writing unit tests, so apologies if this is a dumb question. If I'm trying to test a method in an Angular controller that relies on mock data from a service call, and I want to also mock that service call (ngResource), is there a way to make httpBackend ignore other requests made in my controller on initialization?
I've tried placing my whenGET or expectGET definitions in before blocks, and only instantiating my controller within my test, but I always find that httpBackend is expecting other requests (Error: Unexpected request) when I call flush(). I do not want to write mocks for all other requests, just the one I'm using for this test.
Of course, this may be a stupid idea, as I can also just provide the fake data directly, and not test the service along with the controller's method. I've verified that this works. Maybe the correct answer is that I shouldn't be testing services from within a controller.
FWIW, I've also tried using Sinon fakeServer, and apparently it doesn't even pick up on Angular's XHR implementation (the server never responds).

Resources