2016-06-23 11 views
1

AngularJSでグラフをレンダリングします。関数はAngularJSで未定義を返します

サービスTrendChartFactoryは、入力として2つのパラメータを持つデータの取得要求を出します。

は今、私の問題は、私は私はそれが未定義を返すget要求から受信したデータを利用した機能getValue()を呼び出すときことです。

この問題を解決するにはどうすればよいですか?

ありがとうございます。

angular.module('App') 
.directive('Trend',function(){ 
    return { 
     restrict: 'E', 
     templateUrl: 'app/trend/trend.html', 

     scope: { 
      card: '=', 
      puName: '<', 
      selectedItem: '<' 
     }, 
     controller: function($scope, $rootScope, $filter, TrendChartFactory) { 

      $scope.$watch('selectedItem', function() { 
       console.log($scope.selectedItem); 
       console.log("ChangedSelected"); 
       TrendChart();    
      }, true); 


      $scope.$watch('puName', function(){ 
       console.log($scope.puName); 
       console.log("ChangedpuName"); 
       TrendChart(); 
      },true); 

      $scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012']; 



      function getValue(selectedV,trendV) { 

        for(var i= 0; i< trendV.length;i++) { 

         console.log(trendV[i][selectedV]); 
         $scope.data.push(trendV[i][selectedV]); 
        } 

        $scope.datta.push($scope.data); 
        console.log($scope.datta); 
      } 

      function TrendChart(){ 

        $scope.data = []; 
        $scope.datta = []; 

        TrendChartFactory.get({ 
        Item: $scope.selectedItem.key, 
        puItem: $scope.puName},function(data){ 

         $scope.trendValues = data;       
         console.log($scope.trendValues); 

        }); 
        getValue($scope.selectedItem.key,$scope.trendValues); 

      } 
     }, 
     controllerAs: "TrendCtrl" 
    }; 
}) 
+0

getValueの代わりに$ scope.getValueを追加する必要があります – user2323308

答えて

0

getValue($scope.selectedItem.key,$scope.trendValues);TrendChartFactory内部のコールバックが実行される前に実行されます。

この命令を内部に配置する必要があります。

TrendChartFactory.get({ 
    Item: $scope.selectedItem.key, 
    puItem: $scope.puName},function(data){ 

    $scope.trendValues = data;       
    console.log($scope.trendValues); 
    getValue($scope.selectedItem.key,$scope.trendValues); 

}); 
関連する問題