嗨,这是我第一次来这里,我是 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/