首页 > 解决方案 > 带有 ui.bootstrap 的 Angularjs 和 jquery.datatable - 需要使用循环在 json 中显示条目

问题描述

我需要在循环中显示 json 的条目。我是 js 和 angular 的新手,我不知道如何在下面代码的标记位置输入 for。我需要知道做所有的代码,这只是一个更大的项目的测试。

        <?php
        $estudent[]=array(
        "name" => "luis", 
        "age" => "10");
        $estudent[]=array(
        "name" => "maria", 
        "age" => "12");
         $objJson=json_encode($estudent);
        ?>
    //here is the js
    <script>
    var json=eval(<?php echo $objJson; ?>);
    //Angularjs and jquery.datatable with ui.bootstrap and ui.utils

    var app=angular.module('formvalid', ['ui.bootstrap','ui.utils']);
    app.controller('validationCtrl',function($scope){

 $scope.data = [
            [ //i need to use a loop for show all the studens
                json[0].name,
                json[0].age,
                    ],
             [ //i need to use a loop for show all the studens
                json[1].name,
                json[1].age,
                   ],
        ]

    $scope.dataTableOpt = {
       //custom datatable options 
      // or load data through ajax call also
      "aLengthMenu": [[10, 50, 100,-1], [10, 50, 100,'All']],
      };
    });
    </script>

标签: javascriptangularjsjsonfor-loop

解决方案


对于此循环,您可以使用:

$scope.data = [];
angular.forEach(json, (item) => {
  // here you can get item.age  and item.name
  $scope.data.push(item);
});

在此循环结束时,您$scope.data将拥有所有学生


推荐阅读