(角http$):无法读取未定义的属性'length'

[英](Angular http$): Cannot read property 'length' of undefined


I am working on an apps to read JSON file.

我正在开发一个读取JSON文件的应用程序。

Currently I am able to retrieve and read the JSON file but the problem is when to choose data in JSON.

目前我可以检索和读取JSON文件,但问题是何时选择JSON中的数据。

Error "Cannot read property 'length' of undefined"

错误“无法读取未定义的属性'length'”

Below is my code.

下面是我的代码。

Services.js

Services.js

.factory("mainData", function($http, $log, $q) {
    var chartData;

    return {

        all: function() {
            var d = $q.defer();
            $http({ method: 'GET',
                url: 'http://localhost:8080/aZolla/getGlazCharts.do'})
                    .success(function (data, status, header, config) {
                         d.resolve(data);
                        })
                    .error(function (data, status, header, config) {
                        $log.warn(data, status, header, config);
            });

            chartData = d.promise;

            return d.promise;     
        },

       get: function(chartID) {
        for (var i = 0; i < chartData.length; i++) {
        if (chartData[i].id === parseInt(chartID)) {
          return chartData[i];
        }
      }
      return null;
    }
 };
});

Controller.js

Controller.js

.controller("ChartsListsCtrl", function($scope, mainData) {

    mainData.all().then(function(data){
    $scope.chartLists = data;
    }            
   )

.controller('ChartsCtrl', function($scope, $stateParams, mainData) {

  $scope.chart = mainData.get($stateParams.chartId);

})

2 个解决方案

#1


0  

You should pass chartData to your get function. Try this:

您应该将chartData传递给您的get函数。试试这个:

get: function(chartID, chartData) {
  for (var i = 0; i < chartData.length; i++) {
    if (chartData[i].id === parseInt(chartID)) {
      return chartData[i];
  }
}

In controller:

控制器:

.controller('ChartsCtrl', function($scope, $stateParams, mainData) {
  mainData.all().then(function(data){
    $scope.chart = mainData.get($stateParams.chartId, data);
  });
})

#2


1  

Try This

试试这个

.controller("ChartsListsCtrl", function($scope, mainData) {

    mainData.all().then(function(data){
    $scope.chartLists = data;
    }            
   )

   .controller('ChartsCtrl', function($scope, $stateParams, mainData) {
      mainData.all().then(function(data){
       $scope.chart = mainData.get($stateParams.chartId);
      })
   })

You need to load all before get call. In your first controller all call being happened. But it does not mean in your second controller it will be available, because your second controller may be initialized same time with your first controller and till then may be your all call is not getting finished.

你需要在接到电话之前加载所有文件。在第一个控制器中,所有调用都发生了。但它并不意味着在你的第二个控制器中它是可用的,因为你的第二个控制器可以与你的第一个控制器同时初始化,直到那个时候你的所有调用都没有完成。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2016/05/27/86e619c943cda6db3e4883fb8e271f7f.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告