最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - angular dot notation better explanation - Stack Overflow

programmeradmin1浏览0评论

I was looking for a solution about polling data using AngularJS and I found here at stackoverflow.

In this solution (shown bellow) it is used a javascript object to return the response (data.response) and if I try to replace that data object for a simple javascript array it doesn't work, I would like to know exactly why I need to go with dot notation and why a single array doesn't work? (It would be great links or explanation with 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
  };
});

Trying to summarize my goal (what I want to really understand): where is var data = { response: {}, calls: 0 }; could be var data = {}; and then the response.data would be setted directly to data data = r.data and return {data: data};, why do I need to rely on dot notation?

I was looking for a solution about polling data using AngularJS and I found here at stackoverflow.

In this solution (shown bellow) it is used a javascript object to return the response (data.response) and if I try to replace that data object for a simple javascript array it doesn't work, I would like to know exactly why I need to go with dot notation and why a single array doesn't work? (It would be great links or explanation with 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
  };
});

Trying to summarize my goal (what I want to really understand): where is var data = { response: {}, calls: 0 }; could be var data = {}; and then the response.data would be setted directly to data data = r.data and return {data: data};, why do I need to rely on dot notation?

Share Improve this question edited Aug 15, 2017 at 22:02 Machavity 31.7k27 gold badges95 silver badges105 bronze badges asked Aug 4, 2013 at 5:14 Leandro MoreiraLeandro Moreira 3773 silver badges16 bronze badges 5
  • I remove the dot, it still works plnkr.co/edit/lIltfOWL4YObm5oRB13W?p=preview – zs2020 Commented Aug 4, 2013 at 5:26
  • But you still access response trough data.response I meant: instead of var data = { response: {}, calls: 0 }; be var data = []; – Leandro Moreira Commented Aug 4, 2013 at 5:29
  • I tried to replace on your example and it is failing now plnkr.co/edit/kCV95VJlCpSq6etVNJVf?p=preview – Leandro Moreira Commented Aug 4, 2013 at 5:32
  • Because every time when poller is called, data is pointing to a new object data = r.data; – zs2020 Commented Aug 4, 2013 at 5:37
  • ohh I think I get it. Since I'm using the same object instance (data.prop = value) the only thing will change each time is the value and if I try to use [], {} or a simple value it makes me lose the reference in the view. (binding: view is pointing to an object that doesn't exist anymore). Did I get it right? – Leandro Moreira Commented Aug 4, 2013 at 6:00
Add a ment  | 

1 Answer 1

Reset to default 7

Let's say if we change the factory in this way:

app.factory('Poller', function($http, $timeout) {
  var d = {};
  var poller = function() {
    $http.get('data.json').then(function(r) {
      d = r.data;
      $timeout(poller, 1000);
    });
  };
  poller();

  return d;
});

In the controller, the statement $scope.data = Poller; assign d object to $scope.data, so the object relationship is like this after the initialization

$scope.data -> d -> r.data

When poller() is called again in 1 sec, d is replaced with an new object, so the object relationship will be

$scope.data -> d* -> r.data (d* is a new object)

so the angularjs's data binding will be broken since there is no way to trace to the r.data since d* is a brand new object with different prototype.

With dot notation, after the initialization the object relationship never changes since the periodic calls to poll() doesn't create new object d but it just keeping updating the response field with new r.data object.

$scope.data -> d.response -> r.data
发布评论

评论列表(0)

  1. 暂无评论