通过嵌套ng-repeat中的键访问对象

问题描述 投票:0回答:1

是否无法使用ng-repeat中的键访问另一个对象的值?

$scope.top = {{assignment:'keyOne'}...};
$scope.first = {1: { keyOne: {a:'b'}}, 2: {keyOne:{b:'c'}}};
$scope.firstKeys = [1,2]; // built from Object.keys($scope.first); 

[$scope.top包含对象的对象,我遍历$scope.top,试图将assignment属性的值用作键:

<span ng-repeat="line in top>
    <span ng-repeat "key in firstKeys">
      {{first[key][line.assignment]}}
    </span>
</span>

在上面的示例中,我希望{{first[key][line.assignment]}}可以打印出$scope.first中的两个对象,但实际上这似乎并不起作用。

这可能吗,还是这里有更好的使用方法?

angularjs
1个回答
0
投票

您的代码包含许多错误记录和错误,以下代码有效:

  $scope.top = [{assignment:'keyOne'}];
  $scope.first = {1: { keyOne: {a:'b'}}, 2: {keyOne:{b:'c'}}};
  $scope.firstKeys = [1,2]; // built from Object.keys($scope.first);


   <span ng-repeat="line in top>
    <span ng-repeat "key in firstKeys">
      {{first[key][line.assignment]}}
    </span>
  </span>

输出:{“ a”:“ b”} {“ b”:“ c”}

© www.soinside.com 2019 - 2024. All rights reserved.