如何获取新的ng-repeat项目的索引?

时间:2017-11-07 00:55:16

标签: angularjs angularjs-ng-repeat

我正在摸索角度,我想知道用当前索引添加标签的最佳方法是什么。

目前我有:

$scope.setups = [];

$scope.add = function () {
 $scope.setups.push({ 
    panelVal: "",
    spacingVal: "",
    lotsVal: "",
    panelPlaceholder: "Panels per lot",
    spacingPlaceholder: "Between frames (mm)",
    lotsPlaceholder: "Number of Lots"
    });
};

$scope.remove = function(array, index){
 array.splice(index, 1);
}

我尝试将添加功能更改为:

$scope.add = function (index) {
 $scope.setups.push({ 
    label: "Setup "+index,
    panelVal: "",
    spacingVal: "",
    lotsVal: "",
    panelPlaceholder: "Panels per lot",
    spacingPlaceholder: "Between frames (mm)",
    lotsPlaceholder: "Number of Lots"
    });
};

但索引未定义。我假设我应该得到设置索引。或者甚至可能只是当前的长度并将其递增1。什么是最好的解决方案?

1 个答案:

答案 0 :(得分:0)

如果它可以帮助其他人,我就可以解决这个问题。要添加标签,我得到了总长度,然后只是添加到删除功能,以便在项目删除时重新标记。

$scope.setups = [];

$scope.add = function (array, index) {

 arrlength = array.length;
 arrlength++;

array.push({ 
    label: "Setup "+arrlength,
    panelVal: "",
    spacingVal: "",
    lotsVal: "",
    panelPlaceholder: "Panels per lot",
    spacingPlaceholder: "Between frames (mm)",
    lotsPlaceholder: "Number of Lots"
    });
};

$scope.remove = function(array, index){
 array.splice(index, 1);

 labels = document.getElementsByClassName("setup-label");

 for(var i = 0; i < labels.length; i++)
 {
    labels[i].textContent="Setup "+i;
 }
}