在 ng-repeat 值中使用动态值

Use a dynamic value in ng-repeat value

如何根据 filterOption 获取要迭代的对象中的值?宽度还是高度?我需要绑定到模型吗?大多数其他类似的问题都与模型有关。

目标: ng-repeat="section in prefs.filter.DYNAMIC_VALUE_FROM_filterOption

不输出任何东西: ng-repeat="section in prefs.filter[0]

成功但不是动态的 - 宽度是硬编码的。 ng-重复="section in prefs.filter.width"

FIDDLE http://jsfiddle.net/xyp6y1ap/2/

JAVASCRIPT

function MainCtrl($scope) {

    $scope.prefs = {
        atts: ['width', 'height'],
        filter: {
            width: ['160', '180', '300', '728'],
            height: ['90', '150', '250', '600'],

        },
        filterOption: 1

    };

}

中继器在 HTML

       ng-repeat="section in prefs.filter.DYNAMIC_VALUE"

       ng-repeat="section in prefs.filter.width"

就像您在 JavaScript 中所做的那样。

我假设

$scope.prefs.filterOption 是您要迭代的属性的数组 $scope.prefs.atts 中的索引。

所以属性的名称是$scope.prefs.atts[$scope.prefs.filterOption]

因此您要迭代的实际数组是

$scope.prefs.filter[$scope.prefs.atts[$scope.prefs.filterOption]]

所以你想要,在你的 HTML

ng-repeat="section in prefs.filter[prefs.atts[prefs.filterOption]]"

不过,这非常令人费解。控制器的要点是为视图提供一个易于使用的模型。不是动态设置索引的值,它本身允许访问属性的名称,它本身允许访问宽度或高度的数组,您应该简单地动态设置一个指向数组本身的变量。

您可以在 ng-repeat 中使用键值对,例如:

<section ng-repeat="item in prefs.filter">
    <section ng-repeat="(key,value) in item">
        {{value}}
    </section>
</section>

http://plnkr.co/edit/IPY2YUWLlquWKI7I5MM2?p=preview