如何使用ngRepeat中的值作为访问作用域的动态方式



我有一组要在视图中动态生成的值。我该如何按照以下方式进行呢?

$scope.mainKeyMapping = [
    {
        name: 'category1',
        otherThings: ''
    }, 
    {
        name: 'category2',
        otherThings: ''
    }, 
    {
        name: 'category3',
        otherThings: ''
    }, 
    {
        name: 'category4',
        otherThings: ''
    }, 
    {
        name: 'category5',
        otherThings: ''
    }
];

$scope.category1 = {something....}
$scope.category2 = {something....}
$scope.category3 = {something....}
$scope.category4 = {something....}
$scope.category5 = {something....}

HTML

<div ng-repeat="cat in mainKeyMapping">

    {{category1}} // outputs obj
    {{cat.name}} // outputs string "category1" <----- how do I output the obj?

</div>

首先,将您的类别放入一个集合:

$scope.categories = {
 category1: {something....},
 category2: {something....}
};

现在只需访问您的html:中的正确类别

<div ng-repeat="cat in mainKeyMapping">
  {{categories.category1}} // outputs obj
  {{categories[cat.name]}} // outputs obj
</div>

最新更新