AngularJS - 根据输入文本框更改替换数组中的值

时间:2022-08-08 13:18:41

I have a simple text box -

我有一个简单的文本框 -

<input type="text" class="form-control" id="inputID" name="ItemId" ng-model="inputItemId" ng-required="true" ng-blur="addValueToArray(inputItemId)"/>

The number of input boxes increase and decrease based on the user's choice (I have a simple add/subtract functionality that replicates the text boxes or removes them), but they ultimately are all stored in an array -

输入框的数量根据用户的选择而增加或减少(我有一个简单的加/减功能,复制文本框或删除它们),但它们最终都存储在一个数组中 -

        $scope.itemIDs = [];
        $scope.addValueToArray = function(inputItemId)
        {       
            $scope.validID = $filter('uppercase')(inputItemId); 
            $scope.itemIDs.push($scope.validID);        
        }

My $scope.itemIDs array holds all the IDs the user has entered in the various text boxes.

我的$ scope.itemIDs数组包含用户在各种文本框中输入的所有ID。

Say the values in this array right now are - ABC,ABD,ABE,ABZ for four different items.

假设现在这个数组中的值是 - ABC,ABD,ABE,ABZ,用于四个不同的项目。

What I wish to achieve now is, if a user decides to remove the second value ABD and replace it with ABW in the text box, based on how my function works, it ends up adding it after the last element of the array and looks like - ABC,ABD,ABE,ABZ, ABW.

我现在想要实现的是,如果用户决定删除第二个值ABD并在文本框中用ABW替换它,根据我的函数的工作方式,它最终会在数组的最后一个元素之后添加它并看起来像 - ABC,ABD,ABE,ABZ,ABW。

Is there a way in angular where I could replace the entered value of the second value with the new one in the array instead of adding it in the end? Am I missing out on something?

有角度的方法我可以用数组中的新值替换第二个值的输入值而不是最后添加它吗?我错过了什么吗?

1 个解决方案

#1


0  

Try something like this, $scope.itemIDs = []; $scope.addValueToArray = function(oldValue,newValue){ $scope.itemIDs[$scope.itemIDs.indexOf(oldValue)] = newValue; }

试试这样的事情,$ scope.itemIDs = []; $ scope.addValueToArray = function(oldValue,newValue){$ scope.itemIDs [$ scope.itemIDs.indexOf(oldValue)] = newValue; }

#1


0  

Try something like this, $scope.itemIDs = []; $scope.addValueToArray = function(oldValue,newValue){ $scope.itemIDs[$scope.itemIDs.indexOf(oldValue)] = newValue; }

试试这样的事情,$ scope.itemIDs = []; $ scope.addValueToArray = function(oldValue,newValue){$ scope.itemIDs [$ scope.itemIDs.indexOf(oldValue)] = newValue; }