ng-repeat:对象数组中每个对象的访问键和值 [英] ng-repeat: access key and value for each object in array of objects

查看:28
本文介绍了ng-repeat:对象数组中每个对象的访问键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组,我正在使用 ng-repeat 来迭代它们,这很容易.数组看起来像这样:

$scope.steps = [{公司名称:真实},{业务类型:真实},{物理地址:真实}];

我的 ng-repeat 属性看起来像:

... </div>

在每次迭代中,正如预期的那样,step 等于其中一个对象.无论如何都可以访问 step 对象的键和值吗?这样我就可以做这样的事情:

... </div>

其中 stepName == 'companyName'isComplete == true.我试过做这件事,但 stepName 总是最终成为 step 对象的索引(这是有道理的).我只是想弄清楚是否有另一种方法来编写 ng-repeat 语法,以便我可以获取键和值.

感谢您的任何想法/建议.非常感谢.

附注.我目前的工作是在我的控制器中执行此操作:

$scope.stepNames = [];angular.forEach($scope.steps, function(isComplete, stepName){$scope.stepNames.push({stepName: stepName, isComplete: isComplete});});

然后对其进行迭代,但最好在视图中完成所有操作

解决方案

中继器内的中继器

<div ng-repeat="(key, value) in step">{{核心价值}}

I have an array of objects and I am using an ng-repeat to iterate over them, which is easy. The array looks something like this:

$scope.steps = [
    {companyName: true},
    {businessType: true},
    {physicalAddress: true}
];

And my ng-repeat attribute looks like:

<div ng-repeat="step in steps"> ... </div>

In each iteration, step is equal to one of the objects, as expected. Is there anyway to access both the key and the value of the step object? So that I could do something like this:

<div ng-repeat="(stepName, isComplete) in steps"> ... </div>

Where stepName == 'companyName' and isComplete == true. I've tried doing this exact thing but stepName always just ends up being the index of the step object (which makes sense). I'm just trying to figure out if there is another way to write the ng-repeat syntax so that I can get the key and the value.

Thanks for any ideas/suggestions. Much appreciated.

PS. My current work around is to just do this in my controller:

$scope.stepNames = [];
angular.forEach($scope.steps, function(isComplete, stepName){
     $scope.stepNames.push({stepName: stepName, isComplete: isComplete});
});

And then to iterate over that, but it would be nice to do it all in the view

解决方案

A repeater inside a repeater

<div ng-repeat="step in steps">
    <div ng-repeat="(key, value) in step">
        {{key}} : {{value}}
    </div>
</div>

这篇关于ng-repeat:对象数组中每个对象的访问键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆