2016-06-23 52 views
1

我想在AngularJS中渲染图表。函数在AngularJS中返回undefined

服务TrendChartFactory发出获取请求来检索数据,有两个参数作为输入。现在

我的问题是,当我打电话,让我使用从GET请求接收数据则返回undefined的功能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

您需要添加$ scope.getValue而不是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); 

});