Hello
I have a loop (ng-repeat) in my html file and would like to save the index of it in a variable to be able to pass parameter to a function.
Does anyone have an idea how I can do this?
Follow the code in View:
<div class="lista_cores">
<div class="opcao_tamanho col-xs-12" ng-repeat="tamanho in vm.tamanho">
{{vm.index = $index}}
<div class="lista_cores" ng-repeat="cor in tamanho.cor track by $index">
<button type="button" class="btn_remove" ng-click="vm.removeCor(vm.index, $index)"><span class="glyphicon glyphicon-remove"></span></button>
</div>
<button type="button" class="btn_adiciona" ng-click="vm.addCor($index)">Adicionar cor</button>
</div>
<button type="button" class="btn_remove" ng-click="vm.removeTamanho($index)"><span class="glyphicon glyphicon-remove"></span></button>
</div>
<button type="button" class="btn_adiciona" ng-click="vm.addTamanho($index)">Adicionar tamanho</button>
Function on Controller:
vm.tamanho = [{
"nome_produto_tamanho": "",
"cor": [{
"nome_produto_cor": ""
}]
}];
vm.addTamanho = function(index){
vm.tamanho.push({
"nome_produto_tamanho": "",
"cor": [{
"nome_produto_cor": ""
}]
});
}
vm.removeTamanho = function(index) {
vm.tamanho.splice(index, 1);
};
vm.cores = {
"nome_produto_cor": ""
};
vm.addCor = function(id_tamanho){
vm.tamanho[id_tamanho].cor.push(vm.cores);
}
vm.removeCor = function(id_tamanho, index) {
console.log(index);
console.log(id_tamanho);
vm.tamanho[id_tamanho].cor.splice(index, 1);
};
The code has been greatly simplified, but it's basically the part that matters.
The user can add color and size by clicking a button and remove also, my problem is when removing an added color, since I need the size id to be able to delete the color.