Javascript ng repeat$index内的ng repeat
嗨,这是我第一次来这里,我是Angularjs的新手,如果这里有人能帮我解决代码中的问题,我将不胜感激。我嵌套了类似这样的Javascript ng repeat$index内的ng repeat,javascript,angularjs,Javascript,Angularjs,嗨,这是我第一次来这里,我是Angularjs的新手,如果这里有人能帮我解决代码中的问题,我将不胜感激。我嵌套了类似这样的ng repeat(一个用户可能有多个comptes,一个compte也可能有多个mandat,当我单击AddMandatItem时,它添加了一个,这是使用添加的,但当我第二次单击AddMandatItem时(在另一个compte上),它添加了两个项目(第一个加在第二个之前)。我可以参考母公司,以便我的添加我想为每个公司添加的mandat数量吗 <button ng-c
ng repeat
(一个用户可能有多个comptes,一个compte也可能有多个mandat,当我单击AddMandatItem
时,它添加了一个
,这是使用
添加的,但当我第二次单击AddMandatItem
时(在另一个compte上),它添加了两个项目(第一个加在第二个之前)。我可以参考母公司,以便我的
添加我想为每个公司添加的mandat数量吗
<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>
您可以使用一些变量作为索引,也可以使用。有关更多详细信息,请参阅
<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>
添加一个compte
Compte{{parentIndex+1}
身份证件
唯一引用
身份证件
为该compte添加mandat
根据您的要求,我相信您希望这样做:
HTML:
<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="compteItem.id" />
<label>Unique reference</label>
<input type="text" ng-model="compteItem.uniqueRef" />
<div ng-repeat="mandat in compteItem.mandats track by $index">
<label>Id</label>
<input ng-model="mandat.id" />
<div>
<button ng-click="addMandatItem(compteItem)">Add a mandat for that compte
</button>
</div>
我找到了这个解决方案,我分享它来帮助其他有同样问题的人:这是我的App.js
$scope.addMandat = function(i){
if($scope.compteItems[i].mandats==null){
$scope.compteItems[i].mandats=[];
}
console.log(i);
var newMandat = {};
$scope.compteItems[i].mandats.push(newMandat);
};
以及:
添加一个compte
Compte{{parentIndex+1}
身份证件
唯一引用
身份证件
为该compte添加mandat
Hi,感谢您尝试帮助我,但我的app.js中仍然存在问题,因为我声明了$scope.mandats而不是compte[parentIndex].mandats!我编辑了我的app.js,但仍然出现错误:$scope.compte.mandats=[];
控制台.log(I);
控制台.log(I)
var newMandat={};
$scope.compte[i].mandats.push(newMandat);
};
谢谢你的回答,我用@Arumuga Raja help找到的正确答案重播了一遍
$scope.compteItems = [{}];
$scope.addCompteItem = function(){
$scope.compteItems.push({mandats:[]});
};
$scope.addMandat = function(compteItem){
var newMandat = {};
compteItem.mandats.push(newMandat);
};
$scope.addMandat = function(i){
if($scope.compteItems[i].mandats==null){
$scope.compteItems[i].mandats=[];
}
console.log(i);
var newMandat = {};
$scope.compteItems[i].mandats.push(newMandat);
};
<button ng-click="addCompteItem()">Add a compte</button>
<div ng-repeat="compte 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.mandats" ng-init="childIndex = $index">
<label>Id</label>
<input ng-model="compte.mandat[childIndex].id" />
</div>
<div>
<button ng-click="addMandatItem(parentIndex)">Add a mandat for that compte
</button>
</div>
</div>