2015-08-20 79 views
1

如果我有這樣的:是否有可能通過對象的指定列表NG-重複屬性

$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]; 
}); 

回答

1

嘗試改變,

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

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

使用括號符號就像你會在正常的JavaScript代碼:

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