是否可以通过指定的对象属性列表进行ng-repeat

时间:2015-08-20 21:22:01

标签: javascript angularjs

如果我有类似的话:

$scope.listOfAttributes = ["someThings", "yetMoreThings"]     

$scope.whatever = {
    someThings: "stuff",
    otherThings: "Also stuff",
    yetMoreThings: "still stuff"
};

是否可以做这样的事情:

<ul>
  <li ng-repeat="thing in listOfAttributes">{{ whatever.thing }}</li>
</ul>

或者我必须先处理它:

$scope.generatedObject = {}
$scope.listOfAttributes.forEach(function(thing)){
    $scope.generatedObject[thing] = $scope.whatever[thing];
});

2 个答案:

答案 0 :(得分:2)

使用括号表示法就像在普通的javascript代码中一样:

<ul>
    <li ng-repeat="thing in listOfAttributes">{{ whatever[thing] }}</li>
</ul>

答案 1 :(得分:1)

尝试改变,

<li ng-repeat="thing in listOfAttributes">{{ whatever.thing }}</li>

<li ng-repeat="thing in listOfAttributes">{{ whatever[thing] }}</li>