如何使用ng repeat angularjs在html中打印数据



我在控制器中有json数据作为响应。我必须用html打印这些数据。

为此,我做了如下:

在我的控制器中

.controller('DataImportControl', ['$scope','$http', '$location', '$rootScope' , function($scope, $http, $location, $rootScope) {
    console.log(" In dataimportCtrl");
        $scope.readCustomer = function(){
            console.log("in read customer");
            $http.post('/custimport').success(function(response) {

                console.log("in controller");
                console.log("controller:"+response);
                $scope.data=response; 
            }).error(function(response) {
             console.error("error in posting");
            });
        };
}])

我的html代码:

<html>
<head>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<div ng-app="" ng-controller="DataImportControl">
<form ng-submit="readCustomer()">
    <button type="submit" >Submit</button>
    </form>
<table class="table">
    <thead>
  <tr>
<th>Code exists in customer master</th>
  </tr>
    </thead>
<tbody>
<tr ng-repeat="cust in data ">
    <td>{{cust.customer_code}}</td>
</tr>
</tbody>
</table>
</div>
<div >
</div>
</body>
</html>

我的json数据作为控制器中的响应:

{"jarray":[{"customer_name":["Ankita"],"customer_code":["c501"]}]}

我的问题是,在我的情况下,如何使用ng repeat在html中打印json数据?

提前感谢。。。

我假设您想打印出实际的对象,在这种情况下,只需进行

{{cust|json}}

将其包装在预标签中,使其进行美化。

从服务返回的数据格式很奇怪。我假设您有多条记录被返回,但在本例中只显示了一条。

您在这里显示的记录是一个对象,其中一个属性是数组。目前还不清楚你的对象是否有多个数组,或者这个数组中是否嵌入了多个客户对象。然而,我制作了一个快速的plunker,展示了如何在这两种情况下迭代。

首先,如果您打算直接遍历data,并且data将包含多个数组,那么您将使用(key, value)语法,因为data本身是一个对象,而不是数组。

<div ng-repeat="(key, value) in data">
  key: {{key}}
  <br/>value: {{value}}
  <div ng-repeat="customer in value">
    Customer name: {{customer.customer_name}}
    <br/>Customer code: {{customer.customer_code}}
  </div>
</div>

但是,如果您的数据只返回一个对象属性jarray,并且您将始终在同一属性中迭代,则外部ng-repeat是不必要的:

<div ng-repeat="customer in data.jarray">
  Customer name: {{customer.customer_name}}
  <br/>Customer code: {{customer.customer_code}}
</div>

如果jarray对数据没有特定意义,您可能需要清理您的服务,无论是在角度上还是在服务器上,并将其全部删除。

http://plnkr.co/edit/Nq5Bo18Pdj4yLQ13hnrJ?p=preview

最新更新