2016-11-07 54 views
-2

下面的函數返回此JSON對象:如何使用推一個循環內

[{"number":2,"des":"Ceintures De Sécurités Passagères","la_date":1506960648000},{"number":7,"des":"Ceintures De Sécurité Conducteur","la_date":1509542654000},{"number":1,"des":"Ceintures De Sécurités Passagères","la_date":1512134781000}]

功能:

$scope.loadDataFromToMonth= function (from,to,year) { 
    var url = servername+'admin/dashboard/getIncidentDepartByMonthFromTo/'+from+'/'+to+'/'+year; 
    alert(url); 
    function onSuccess(response) { 
     console.log("+++++getIncidentDepartByMonthFromTo SUCCESS++++++"); 
     if (response.data.success != false) { 
      $scope.payloadgetIncidentDepartByMonthFromTo = response.data.data; 
      var getIncidentDepartByMonthFromTo= $scope.payloadgetIncidentDepartByMonthFromTo; 
      console.log(JSON.stringify(getIncidentDepartByMonthFromTo)); 
      $scope.labels = []; 
      // charCtrl.toto = []; 
      // $scope.data = {}; 
      //$scope.qsd.push('ff'); 
      getIncidentDepartByMonthFromTo.forEach(function(data) { 
       // charCtrl.toto.push($filter('monthName')(monthNumber)); 
       var monthNumber=$filter('date')(data.la_date, "MM"); 
       //$scope.labelsf.push($filter('monthName')(monthNumber)); 
       var mun=data.number; 
       //$scope.dataf.push(data.number); 
       $scope.data = { 
        labels: [monthNumber], 
        datasets: [ 
         { 
          label: 'My First dataset', 
          fillColor: 'rgba(220,220,220,0.2)', 
          strokeColor: 'rgba(220,220,220,1)', 
          pointColor: 'rgba(220,220,220,1)', 
          pointStrokeColor: '#fff', 
          pointHighlightFill: '#fff', 
          pointHighlightStroke: 'rgba(220,220,220,1)', 
          data: [data.number] 
         } 

        ] 
       }; 
      }); 
     } 
     else { 
      alert("failure"); 
     } 
    }; 
    function onError(response) { 
     console.log("-------getIncidentDepartByMonthFromTo FAILED-------"); 
     //$scope.stopSpin('spinner-0'); 
     console.log(response.data); 
     console.log("Inside getIncidentDepartByMonthFromTo error condition..."); 
    }; 
    //----MAKE AJAX REQUEST CALL to GET DATA---- 
    ajaxServicess.getData(url,username,password, 'GET', '').then(onSuccess, onError); 
}; 

我的問題是,在巴焦數據apprears,剛剛過去的出現json對象是:

{"number":1,"des":"Ceintures De Sécurités Passagères","la_date":1512134781000}

+0

所以你只需要一個for循環邏輯來推送唯一的數據? –

+0

循環工作良好..但我不知道我如何使用推內$ scope.data:{} –

+0

當我這樣做$ scope.data:{labels.push(monthNumber)}它給出了一個錯誤 –

回答

0

基本上你需要創建一個特定的對象並賦值給對象的屬性如b elow:

$scope.data = {}; // new object 
$scope.data.datasets = []; // new array in data object .. 
$scope.data.labels =[]; 
getIncidentDepartByMonthFromTo.forEach(function(data) { 

    var monthNumber = $filter('date')(data.la_date, "MM"); 

    var mun = data.number; 


    $scope.data.labels.push(monthNumber); 

    var dataSetObj = {}; //temp object to push into dataset array.. 
    dataSetObj.data = []; 
    dataSetObj.label= 'My First dataset'; 
    dataSetObj.fillColor='rgba(220,220,220,0.2)'; 
    dataSetObj.strokeColor= 'rgba(220,220,220,1)'; 
    dataSetObj.pointColor= 'rgba(220,220,220,1)'; 
    dataSetObj.pointStrokeColor= '#fff'; 
    dataSetObj.pointHighlightFill= '#fff'; 
    dataSetObj.pointHighlightStroke='rgba(220,220,220,1)'; 
    dataSetObj.data.push(data.number); 

    $scope.data.datasets.push(dataSetObj); 

}); 
+0

謝謝你的答案,但它給了我這個錯誤:angular.js:12416 TypeError:無法讀取未定義的屬性「推」 –

+0

我已經更新了答案現在請嘗試我的壞我錯過了這一行'dataSetObj.data = [] ;' –

+0

@imsiimsi你解決了這個問題嗎? –