3

我有一个对象容器,这些对象由它们的标识索引存储在哈希中。

myObjs = {
  "hello id":{
    foo:"hello foo data",
    bar:"hello bar data"
  },
  "world id":{
    foo:"world foo data",
    bar:"world bar data"
  }
}

我想将 myObjs 中的每个对象绑定到 ui-grid ( http://ui-grid.info/ ) 中的一行。在通用表中,它看起来像:

<table>
    <tr ng-repeat="(id, obj) in myObjs">
      <td>{{id}}</td>
      <td>{{obj.foo}}</td>
      <td>{{obj.bar}}</td>
    </tr>
</table>

一种解决方案是从 myObjs 的内容派生一个新的对象数组,用作 ui-grid 的输入数据,但这意味着我必须维护 myObjs 和派生数组输入之间的绑定。

有没有更自然的方式将 ui-grid 绑定到 myObjs?

4

1 回答 1

-1

你能提供一个小提琴/plunk来显示你正在尝试做什么吗?ng-repeat 可以遍历一个对象,就像它可以遍历一个数组一样,因此不需要从您的对象创建派生数组。

我在这里创建了一个:Plunk 它看起来工作正常,所以我不确定问题是什么。

HTML:

<html ng-app="App">
    <head>
        <title>AngularJS Plunker</title>

        <!-- AngularJS - MVVM Pattern -->
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>

        <!-- Page Specific -->
        <script src="client.js"></script>

    </head>

    <body ng-controller="Controller">

      <table border="1">
        <tr ng-repeat="(id, obj) in myObjs">
          <td>{{id}}</td>
          <td>{{obj.foo}}</td>
          <td>{{obj.bar}}</td>
        </tr>
      </table>

    </body>
</html>

js:

var app = angular.module('App', []);

app.controller('Controller', function ($scope) {

  $scope.myObjs = {
    "hello id":{
      foo:"hello foo data",
      bar:"hello bar data"
    },
    "world id":{
      foo:"world foo data",
      bar:"world bar data"
    }
  }

});
于 2014-12-22T20:38:27.053 回答