如何从嵌套的ng-repeat中获取值

时间:2014-08-27 14:02:14

标签: angularjs angularjs-ng-repeat ng-repeat

ng-repeat="question in questions"
{{$index+1}}
ng-repeat="option in  question.choiceBeanList"
{{questionObj.questionId}} --> Here I have to get both data's

我的问题是如何在第二个ng-repeat中获取数据,如下所示的循环示例。

例如:

for(i=0;i<=10;i++)
{
for(j=0;j<=5;j++)
{
console.log(i+"_"+j)
}
}

4 个答案:

答案 0 :(得分:0)

您可以将ngInit用于此

<div ng-repeat="innerList in list" ng-init="outerIndex = $index">
  <div ng-repeat="value in innerList" ng-init="innerIndex = $index">
     <span class="example-init">list[ {{outerIndex}} ][ {{innerIndex}} ] = {{value}};</span>
  </div>
</div>

查看文档:{​​{3}}

答案 1 :(得分:0)

如果您只想尝试获取索引,可以使用$parent.$index

Json模型

  $rootScope.makes = [
    {
      name: 'BMW',
      models: ['335i', 'M3', 'M4']
    },
    {
      name: 'Mercedes',
      models: ['C250', 'E350']
    }
  ];

嵌套ng-repeats

<ul ng-repeat="make in makes">
  <li ng-repeat="model in make.models">
    ({{$parent.$index}},{{$index}}): {{make.name}} {{model}}
  </li>
</ul>

工作example

答案 2 :(得分:0)

试试这个:

<div ng-repeat="question in questions">
  <div ng-repeat="option in  question.choiceBeanList">
    {{questions.indexOf(question)}}   // To get the index value of FIRST ng-repeat
    {{$index}}  // To get the index value of current ng-repeat 
  </div>
</div>

希望它有所帮助......!

答案 3 :(得分:0)

您可以像这样传递外部ng-repeat数据

<div ng-repeat="question in questions">
   <div ng-repeat="option in question.choiceBeanList">
      {{$parent.$index+1}}_{{$index}}
   </div>
</div>