Angularjs和带有ui.bootstrap的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>

在我看来,您的JSON是有效的,请尝试将JSON的格式更改为类似的格式:

$scope.data = [{
"name": "json[0].name",
"age": "json[0].age"
},
{
"name": "json[1].name",
"age": "json[1].age"
}
]

然后你可以轻松地console或显示

angular.forEach($scope.data, function (value, index) {
console.log($scope.data[index] + ' ' + index);
});

HTML

<ul ng-repeat="json in data">
<li><b>Name: </b> <p>{{json.name}}</p> <b>Age: </b> <p>{{json.age}}</p></li>
</ul>

plunker:http://plnkr.co/edit/s6ix8aEDz0jR3UeXGL6M?p=preview

对于这个循环,您可以使用:

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

在这个循环结束时,你的$scope.data将有所有的学生在中

最新更新