函数 returns 在 AngularJS 中未定义

function returns undefined in AngularJS

我想在 AngularJS 中呈现图表。

服务 TrendChartFactory 发出获取数据的请求,将两个参数作为输入。

现在,我的问题是当我调用函数getValue()时,它使用了我从 get 请求中收到的数据它returns未定义

你知道我该如何解决这个问题吗?

提前致谢。

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"
    };
})

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);

});