如何使用角度中的ng-repeat迭代对象

时间:2021-11-30 20:34:48

I am facing problem in iterating an object,in which i have pushed the same property(value) from array of objects.

我在迭代一个对象时面临问题,我从对象数组中推送了相同的属性(值)。

    {
     id:1,
     id:2,
     id:3
    }

I want to display these id's in different rows of table like: 1 2 3

我想在不同的表格行中显示这些id,如:1 2 3

Any help would be appreciated

任何帮助,将不胜感激

4 个解决方案

#1


1  

Firstly, you can't have the same key repeated in an object. Assuming you have an array of objects called 'idObjects': [{id: 1}, {id: 2}, {id: 3}], you can use ng-repeat on the tr element like this:

首先,您不能在对象中重复相同的键。假设你有一个名为'idObjects'的对象数组:[{id:1},{id:2},{id:3}],你可以在tr元素上使用ng-repeat,如下所示:

<table>
  <tr ng-repeat="idObj in idObjects">
    <td>{{ idObj.id }}</td>
  </tr>
</table>

#2


1  

Try this:

angular.module('myapp', [])
.controller("mycontroller", function($scope) {
    $scope.items = [
      {
        "id": 1        
      }
      ,{
        "id": 2
      }
      ,{
        "id": 3
      }
    ];
  });
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></script>
<div ng-app="myapp" ng-controller="mycontroller" class="container">
  <h4>My items</h4>
  <ul class="list-group">
    <li class="list-group-item" ng-repeat="item in items ">{{item.id}}</li>
  </ul>
</div>

#3


0  

First convert the json object into an array and assign it to a $scope variable and then use ng-repeat to iterate over the array.

首先将json对象转换为数组并将其分配给$ scope变量,然后使用ng-repeat迭代数组。

Controller code:

var jsonObj = { "id1":1, "id2": 2, "id3": 3 }; // your object

// constructing the array with your object;
$scope.idArray = Object.keys(jsonObj).map(function(k) { return jsonObj[k] });

HTML code:

<table>
    <tr ng-repeat="id in idArray track by $index">
        <td>{{ id }}</td>
    </tr>
</table>

Hope this solves the issue.

希望这能解决问题。

#4


0  

var values = { id:1, id:2, id:3 };
var $scope.objToArr = [];
angular.forEach(values, function(value, key) {
  $scope.objToArr.push(value);
});

in your html

在你的HTML中

<tr ng-repeat="idval in objToArr">
    <td>{{ idval}}</td>
  </tr>

it works even for different keys also.

它甚至可以用于不同的键。

#1


1  

Firstly, you can't have the same key repeated in an object. Assuming you have an array of objects called 'idObjects': [{id: 1}, {id: 2}, {id: 3}], you can use ng-repeat on the tr element like this:

首先,您不能在对象中重复相同的键。假设你有一个名为'idObjects'的对象数组:[{id:1},{id:2},{id:3}],你可以在tr元素上使用ng-repeat,如下所示:

<table>
  <tr ng-repeat="idObj in idObjects">
    <td>{{ idObj.id }}</td>
  </tr>
</table>

#2


1  

Try this:

angular.module('myapp', [])
.controller("mycontroller", function($scope) {
    $scope.items = [
      {
        "id": 1        
      }
      ,{
        "id": 2
      }
      ,{
        "id": 3
      }
    ];
  });
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></script>
<div ng-app="myapp" ng-controller="mycontroller" class="container">
  <h4>My items</h4>
  <ul class="list-group">
    <li class="list-group-item" ng-repeat="item in items ">{{item.id}}</li>
  </ul>
</div>

#3


0  

First convert the json object into an array and assign it to a $scope variable and then use ng-repeat to iterate over the array.

首先将json对象转换为数组并将其分配给$ scope变量,然后使用ng-repeat迭代数组。

Controller code:

var jsonObj = { "id1":1, "id2": 2, "id3": 3 }; // your object

// constructing the array with your object;
$scope.idArray = Object.keys(jsonObj).map(function(k) { return jsonObj[k] });

HTML code:

<table>
    <tr ng-repeat="id in idArray track by $index">
        <td>{{ id }}</td>
    </tr>
</table>

Hope this solves the issue.

希望这能解决问题。

#4


0  

var values = { id:1, id:2, id:3 };
var $scope.objToArr = [];
angular.forEach(values, function(value, key) {
  $scope.objToArr.push(value);
});

in your html

在你的HTML中

<tr ng-repeat="idval in objToArr">
    <td>{{ idval}}</td>
  </tr>

it works even for different keys also.

它甚至可以用于不同的键。