2016-06-17 44 views
0

我在AngularJS功能:傳遞變量AngularJS

function TrendChart(){ 

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

     TrendChartFactory.get({kpiItem: $scope.selectedKpiItem.key, puItem: $scope.puName},function(data){ 
      $scope.trendValues = data; 
      console.log($scope.trendValues); 
     }) 

     getKpiValue($scope.selectedKpiItem.key,$scope.trendValues); 

} 

,但我想訪問$ scope.trendValues我的功能外像這樣:

function getKpiValue(selectedKPI,trendV) { 
    for(var i= 0; i< $scope.trendV.length;i++){ 
      console.log($scope.trendV[i][selectedKPI]); 
      $scope.data.push($scope.trendV[i][selectedKPI]); 
    } 
    $scope.datta.push($scope.data); 
    console.log($scope.datta); 
} 

但很明顯,我得到它是未定義的錯誤。我怎樣才能解決這個問題 ?

+0

都在同一個控制器兩者的功能? – Deep

+0

是的,它們具有相同的功能 – blaa

+0

當您調用函數getKpiValue時,您傳遞$ scope.trendValues作爲參數。在getKpiValue中,您可以將trendV簡稱爲trendV而不是$ scope.trendV – Thangadurai

回答

0

可以實現通過angular.element

<div ng-app="ManagerApp"> 
    <div id="controller-id" ng-controller="ManagerCtrl"> 
    </div> 
</div> 

的JavaScript:

var scope = angular.element(document.getElementById("controller-id")).scope(); 

一旦你有範圍訪問使用scope.scopeMethod()& scope.scopeVariable

你的範圍方法和變量

jsfiddle詳細例子:

https://jsfiddle.net/nikdtu/hfz3psuu/

0

您可以使用

var scope = angular.element(yourControllerDOMElement).scope(); 
var yourVariable = scope[yourVariableName];