简体   繁体   English

AngularJS ng-重复一个对象

[英]AngularJS ng-repeat an object

I'm getting an object as the scope.我正在获取一个对象作为范围。 The object looks like this:该对象如下所示: 目的

And my controller looks like this:我的控制器看起来像这样:

    module.controller('ActiveController', ['$scope','$http',
      function($scope, $http) {
        $http({
              method: 'GET',
              url: 'http://localhost:8000/api/order/?format=json'
            }).then(function successCallback(response) {

                console.log("OK Respone");
                console.log(response.data);
                $scope.orders = response.data;
              }, function errorCallback(response) {

                console.log("NO Response");
        });
 }]);

In the browser console, the object looks like this:在浏览器控制台中,该对象如下所示:

控制台中的对象

I would like some help to loop out and display whole the object in the .html file.我需要一些帮助来循环显示 .html 文件中的整个对象。 My current code that does not work is currently looking like this:我当前不起作用的代码目前看起来像这样:

<div ng-controller="ActiveController">
<div ng-repeat="order in orders">
    <p>{{ order.id }}</p>
    <p>{{ order.created }}</p>     
</div>
</div>

I don't think I need to show my "main" .html file, so I'm not posting it.我认为我不需要显示我的“主要”.html 文件,所以我没有发布它。

The problem is in the controller.问题出在控制器上。 Try saving objects in $scope.orders尝试在$scope.orders保存objects

$scope.orders = response.data.objects;

You can fix this in the view and the controller as well:您也可以在视图和控制器中修复此问题:

In View as:在视图中:

<div ng-controller="ActiveController">
  <div ng-repeat="order in orders.objects"> <!-- note the orders.objects -->
    <p>{{ order.id }}</p>
    <p>{{ order.created }}</p>     
  </div>
</div>

In Controller as在控制器中作为

$scope.orders = response.data.objects;

As rightly suggested by @ddepablo.正如@ddepablo 正确建议的那样。 It will work fine.它会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM