从内部ng-repeat数组中按键获取外部ng-repeat对象的值

时间:2017-06-01 09:54:38

标签: angularjs angularjs-ng-repeat

people{
obj1:{
      key1:value1,
      key2:value2,
      key3:value3,
      key4:value4,
      key5:value5
 }
obj2:{
      key1:value1,
      key2:value2,
      key3:value3,
      key4:value4,
      key5:value5
 }
obj3:{
    key1:value1,
      key2:value2,
      key3:value3,
      key4:value4,
      key5:value5
 }
 obj4:{
      key1:value1,
      key2:value2,
      key3:value3,
      key4:value4,
      key5:value5
 }
obj5:{
key1:value1,
      key2:value2,
      key3:value3,
      key4:value4,
      key5:value5
 }
obj6:{
      key1:value1,
      key2:value2,
      key3:value3,
      key4:value4,
      key5:value5
 }
}

keys_array=[key1,key3,key5];

<div ng-repeat="people in peoples" >
   <hr />
   <table style="width:100%">
      <tr ng-repeat="key in keys">
         <td>{{key}}:</td><td >{{device.key}}</td>
      </tr>
   </table>
   <hr />
 </div>

这里我试图通过内部ng-repeat循环数组中的键获取外部ng-repeat循环对象的值。
我不知道哪种方法是正确的。

1 个答案:

答案 0 :(得分:0)

您的示例代码有错误(变量名,语法)。 我已经解决了错误并修改了html,因此更容易编辑

控制器:

$scope.peoples = {
    obj1:{
          key1:'value1',
          key2:'value2',
          key3:'value3',
          key4:'value4',
          key5:'value5'
     },
    obj2:{
          key1:'value1',
          key2:'value2',
          key3:'value3',
          key4:'value4',
          key5:'value5'
     },
    obj3:{
        key1:'value1',
          key2:'value2',
          key3:'value3',
          key4:'value4',
          key5:'value5'
     },
     obj4:{
          key1:'value1',
          key2:'value2',
          key3:'value3',
          key4:'value4',
          key5:'value5'
     },
    obj5:{
    key1:'value1',
          key2:'value2',
          key3:'value3',
          key4:'value4',
          key5:'value5'
     },
    obj6:{
          key1:'value1',
          key2:'value2',
          key3:'value3',
          key4:'value4',
          key5:'value5'
     }    
    }

HTML:

<div ng-repeat="people in peoples" >
     {{people}}
      <span ng-repeat="key in keys_array">
         {{people[key]}}
      </span>
 </div>