javascript - ng-repeat 内部 ng-repeat $index

标签 javascript angularjs

嗨,这是我第一次来这里,我是 Angularjs 的新手,如果有人可以帮助我解决代码中的问题,我将不胜感激。我嵌套了ng-repeat像这样(一个用户可能有多个 comptes,一个 compte 也可能有多个命令,当我单击 AddMandatItem 时,它会添加一个 <div> ,它是使用 <ng-repeat> 添加的,但是当我第二次单击 AddMandatItem 时(在另一个竞赛中)它添加了 2 个项目(之前添加的第一个 + 第二个)。我可以引用父竞赛,以便我的 <ng-repeat> 添加我想要为每个竞赛添加的任务数量吗?

<button ng-click="addCompteItem()">Add a compte</button>
<div ng-repeat="compteItem in compteItems track by $index">
  <h2>
   <strong>Compte {{$index + 1}}</strong>
  </h2>
  <label>Id</label>
  <input type="text" ng-model="compte[$index].id" />
  <label>Unique reference</label>
  <input type="text" ng-model="compte[$index].uniqueRef" />


  <div ng-repeat="mandat in mandats track by $index>
  <label>Id</label>
  <input ng-model="compte[$parent.$index].mandat[$index].id" />

<div>
<button ng-click="addMandatItem($parent.$index,$index)">Add a mandat for          that compte
</button>
 </div>

app.js:

$scope.compteItems = [{}];
$scope.addCompteItem = function(){
    $scope.compteItems.push({});
};

$scope.mandats=[];
$scope.addMandat = function(i,j){
        console.log(i);
        console.log(j);
        $scope.mandats.push([]);
        var newMandat = {};
        $scope.mandats[i][j]=newMandat;
};

最佳答案

你可以使用一些变量作为索引,你可以使用。请参阅AngularJS Documentation了解更多详情

    <button ng-click="addCompteItem()">Add a compte</button>
    <div ng-repeat="compteItem in compteItems" ng-init="parentIndex = $index">
      <h2>
       <strong>Compte {{parentIndex + 1}}</strong>
      </h2>
      <label>Id</label>
      <input type="text" ng-model="compte[parentIndex].id" />
      <label>Unique reference</label>
      <input type="text" ng-model="compte[parentIndex].uniqueRef" />
    
      <div ng-repeat="mandat in compte[parentIndex].mandat" ng-init="childIndex = $index">
        <label>Id</label>
        <input ng-model="compte[parentIndex].mandat[childIndex].id" />
    
      <div>
      <button ng-click="addMandatItem(parentIndex,childIndex)">Add a mandat for that compte
      </button>
    </div>

关于javascript - ng-repeat 内部 ng-repeat $index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43974381/

相关文章:

javascript - 通过网络应用程序打开移动设备共享对话框

javascript - 使用打开/关闭文本显示隐藏多个内容

javascript - 扩展 $resource 对象从 Factory 中移除原型(prototype)

javascript - Puppeteer 中的 XPath 表达式而不是 CSS 选择器?

javascript - 如何让 onclick 函数更改另一个 onclick 函数的属性?

javascript - 如何处理这两个: onClick and onMouseMove for one dom element?

javascript - 如何在与链接函数隔离的范围内设置范围变量

angularjs - Flex in grand(grand)children of row layout

angularjs - 如何在AngularJS中将破折号转换为camelCase?

AngularJS - 如何在 $localStorage 中存储 JWT token