如何使用ng中的值重复作为访问范围的动态方式?

时间:2015-02-25 01:21:38

标签: angularjs

我有一组我希望在视图中动态生成的值。我怎样才能以下列方式去做呢?

$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>

1 个答案:

答案 0 :(得分:1)

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

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

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

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

  {{categories.category1}} // outputs obj

  {{categories[cat.name]}} // outputs obj

</div>