如何在angularjs中循环json对象

时间:2016-11-07 12:00:47

标签: angularjs json ionic-framework

我有这个静态数据:

  $scope.data = {
                    labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
                    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: [75, 59, 80, 81, 56, 55, 40, 84, 64, 120, 132, 87]
                        }

                    ]
                };

我的问题是:我的真实数据来自后端heres返回的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}]

我使用for循环来循环数据:

 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: [num]
                        }

                    ]
                };
            });

但是这个循环只给出了JSON对象中的最后一个结果,我的意思是:

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

我怎样才能获得所有数据?我必须使用这样的推送或咒语??

1 个答案:

答案 0 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body ng-app="test" ng-controller="test">
  <label class="text-xs col-xs-4 switch" for="check-box">
    <input type="checkbox" ng-model="inAtlas" id="check-box">
    <span class="slider round">Toggle me</span>
  </label>
  <br><br>
  <button ng-click="getValue()">Get checkbox value</button>
</body>

您不必为$ scope.data

指定值