循环遍历AngularJS中的JSON元素

时间:2013-05-23 13:05:37

标签: php json angularjs

我有以下ng-repeat代码:

<li ng-repeat="vid in vids | filter:query | orderBy:orderProp">
       {{vid.title}}
</li>

我的问题是,如果我不知道密钥名称,我可以循环访问vid并打印出每个密钥和值吗?

所以我会在php中做到这一点:

<?php
foreach ($vids as $key => $val) {
    echo $key . "=" . $val;
}
?>

1 个答案:

答案 0 :(得分:5)

我完全不确定问题,但它应该有效

<!DOCTYPE html>
<html ng-app="myApp" >
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
</head>
<body  ng-controller="testctrl">
    <div ng-repeat="vid in vids">
        <span ng-repeat ="(key, value) in vid">{{key}} ,{{value}}</span>
    </div>
  <script>
      var app = angular.module('myApp', []);
      app.controller('testctrl', function ($scope) {
          $scope.vids = [{ 'adam': 10, 'amalie': 12 }, { 'adam': 12, 'amalie': 12 }];
      });
  </script>
</body>
</html>