I came across a solution for polling data using AngularJS here on stackoverflow.
In this particular solution (shown below), a javascript object is used to return the response (data.response
). I tried replacing the data
object with a simple javascript array, but it resulted in not working. I am curious as to why I need to use dot notation and why a single array doesn't suffice. It would be great if someone could provide links or explanations supported by examples.
app.factory('Poller', function($http, $timeout) {
var data = { response: {}, calls: 0 };
var poller = function() {
$http.get('data.json').then(function(r) {
data.response = r.data;
data.calls++;
$timeout(poller, 1000);
});
};
poller();
return {
data: data
};
});
To summarize my goal (what I aim to understand): Can
var data = { response: {}, calls: 0 };
be replaced with var data = {};
, setting response.data
directly to data = r.data
, and return {data: data};
? Why must dot notation be relied upon?