在该方法中无法看到对象数组。 (angularjs)

时间:2015-01-29 20:02:59

标签: javascript angularjs

抱歉我的英文。

chill.controller('messagesController', function($scope, $http, $rootScope, $location, $stateParams, $ionicSlideBoxDelegate, $ionicLoading) {

if ($rootScope.userData != undefined) {
    $ionicLoading.show({
        template: 'loading'
    })
    $http.get('http://api.getchill.co/api/v1/messages/index/id_user/'+$rootScope.userData.id_user+'/id_contact/'+$stateParams.id)
    .success(function(data) {
        $scope.messages = data;
        for (var i = 0; i < $scope.messages.response.length; ++i) {
            if ($scope.messages.response[i].type == 'location') {
                var strings = $scope.messages.response[i].content.split(' ');
                var lat1 = strings[0], lat2 = strings[1];
                $http.get('http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat1+','+lat2+'&sensor=false')
                .success(function(data) {
                    $ionicLoading.hide();
                    $scope.messages.response[i].address = data.results[0].formatted_address;
                });
            };
        };
        $ionicSlideBoxDelegate.update();
    });
}
else
{
    $location.path('/login');
};

});

{p} $scope.messages.response[i].addresssuccess()函数中未定义。我无法弄清楚原因。

TypeError: Cannot set property 'address' of undefined
    at messagesController.js:17
    at ionic.bundle.js:17151
    at processQueue (ionic.bundle.js:20962)
    at ionic.bundle.js:20978
    at Scope.$eval (ionic.bundle.js:22178)
    at Scope.$digest (ionic.bundle.js:21994)
    at Scope.$apply (ionic.bundle.js:22282)
    at done (ionic.bundle.js:17439)
    at completeRequest (ionic.bundle.js:17629)
    at XMLHttpRequest.requestLoaded (ionic.bundle.js:17570)

console.log(data):_ https://dl.dropboxusercontent.com/u/99426050/ShareX/2015/01/2015-01-29_23-40-29.png

1 个答案:

答案 0 :(得分:1)

这里的问题是调用成功函数时i的值已经改变。 i现在是数组的长度(因为for循环已经结束)。 $scope.messages.response[i]返回undefined,因为您在最后一个索引之后要求索引处的值。这是由于scoping and closures的行为。

angular.forEach可以帮到你。

chill.controller('messagesController', function($scope, $http, $rootScope, $location, $stateParams, $ionicSlideBoxDelegate, $ionicLoading) {

  if ($rootScope.userData != undefined) {
      $ionicLoading.show({
          template: 'loading'
      })
      $http.get('http://api.getchill.co/api/v1/messages/index/id_user/'+$rootScope.userData.id_user+'/id_contact/'+$stateParams.id)
      .success(function(data) {
          $scope.messages = data;
          angular.forEach($scope.messages.response, function(response, i){
              if (response.type == 'location') {
                  var strings = response.content.split(' ');
                  var lat1 = strings[0], lat2 = strings[1];
                  $http.get('http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat1+','+lat2+'&sensor=false')
                  .success(function(data) {
                      $ionicLoading.hide();
                      response.address = data.results[0].formatted_address;
                  });
              };
          });
          $ionicSlideBoxDelegate.update();
      });
  }
  else
  {
      $location.path('/login');
  };
});