1

For this method call:

$http.get("Some api call").then(function (response) {
                        $scope.data=response.data;
                    });

Suppose the response keeps on updating from time to time and I wish to update the $scope.data property whenever the response is updating without firing the $http.get using timeout or interval methods.

I am not getting any solution for this issue. Please provide your ideas with examples.

4
  • You can use $scope.$watch('data', function(newValue, oldValue){ console.log('being watched oldValue:', oldValue, 'newValue:', newValue); }); Check if value is changed and if yes then call get Commented Jul 31, 2015 at 12:26
  • @neda Can you please provide an example Commented Jul 31, 2015 at 12:27
  • Watch the data variable like my above comment and check if it has changed . Commented Jul 31, 2015 at 12:28
  • If the data is changing on the server, you either need to poll from the client (e.g. through setTimeout/Interval, which you want to avoid), or you can try to use something like WebSockets html5rocks.com/en/tutorials/websockets/basics -- This allows a connection to be made to the server and the server to send updates to the client. Commented Jul 31, 2015 at 12:30

1 Answer 1

0

What you are asking is called Polling.

Your HTTP request will NOT get the latest data unless you fire the request again. Unless you fire the request again, your response will not have the latest data and thus, your data object will also not get updated.

No amount of $watch will suffice. Simply because, unless you poll the server periodically, you will not get the latest data.

This is how HTTP/1.1 behaves. This is soon about to change with HTTP/2

If you do not wish to make periodic requests, then have a look at sockets. You have not mentioned your backend but a Nodejs example can be found at socket.io.

Sign up to request clarification or add additional context in comments.

5 Comments

Can the solution be developed without using any other js
"...any other js". What do you mean by this?
Like node.js or jetty as you have mentioned in your solution
No! I don't think you understand the problem. That is how HTTP behaves. No matter what your backend is. Either poll the server frequently or use sockets as mentioned.
See what all example I have seen on websockets and angularjs, all have involved nodejs/jetty but have never used solely angularjs implementation. Also, for an rest service like "time.jsontest.com", how to form Websockets as I have only seen "ws" as protocol in all the examples

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.