angular dot notation better explanation

北城以北 提交于 2019-12-04 20:29:56

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!