Angular.js ng-repeat list from *.JSON file



我开始学习Angular.js,我有一个小问题。

这是我的hello.js文件:

function Hello($scope, $http) {
  $http.get('URL').
  success(function(data) {
  $scope.response = data
 });}

响应是一个*。我需要解析的json文件。JSON在某种程度上看起来:

"data":[{
   "status":"true",
   "name":"blabla"
},{
   "status":"true",
   "name":"blabla2"
}]

index.html文件我有<div ng-controller="Hello">,我想使所有JSON的状态和名称字段的列表。

我不知道如何使用ng-repeat以便从*中列出所有组件。json文件。你能帮我吗?:)

欢呼,《路加福音》

如果我正确理解了你的问题,并且你想保持你的控制器原样,你应该这样做:

Html:

<ul>
    <li ng-repeat="resp in response.data"> 
        My Status: {{resp.status}} and my Name: {{resp.name}}
    </li>
</ul>

希望我对你有所帮助。

示例:如果您将json数据分配给控制器中的$scope变量,那么这是使用ng-repeat在视图中显示它的一种可能方法:

HTML:

<div ng-app='App' ng-controller="AppCtrl">
  <table>
     <tr>
         <th ng-repeat="(key, val) in data[0]">{{key}}</th>
     </tr>
     <tr ng-repeat="stuff in data">
         <td ng-repeat="(key, val) in stuff">{{val}}</td>
     </tr>
  </table>
</div>

JS:

angular.module('App', [])
.controller('AppCtrl', function($scope) {
    $scope.data = [
        {"status":"true",
          "name":"blabla"},
        {"status":"true",
         "name":"blabla2"}
    ];
});

输出:

<名称/strong> 状态
鼓励性的真实
blabla2真

编辑:更新小提琴:http://jsfiddle.net/nndc91cp/2/


或者你可以这样创建一个列表:
HTML:

<ul>
   <li ng-repeat="stuff in data"> {{stuff.status}} : {{stuff.name}}</li>
</ul>
输出:

  • true: blabla
  • true: blabla2

最新更新