[英]ng-change is fired a second time when selected option is removed from ng-options - AngularJS 1.7
I have a list of sale items and for each sale item there is a dropdown box where you can select from a list of available IDs to assign to the sale item. 我有一个销售商品的列表,每个销售商品都有一个下拉框,您可以在其中从可用ID列表中进行选择,以分配给该销售商品。 The selection must be unique for each sale item.
每个销售项目的选择必须唯一。 If 2 is selected from saleItem1 dropdown, 2 cannot be available in the dropdown for saleItem2.
如果从saleItem1下拉列表中选择2,则在saleItem2的下拉列表中不能使用2。
After selecting an ID from the dropdown list I need to remove it from the list and replace it with the old ID. 从下拉列表中选择一个ID之后,我需要将其从列表中删除,然后将其替换为旧ID。 Selecting an ID fires the ng-change which updates the list correctly.
选择一个ID会触发ng-change,它会正确更新列表。 However the line marked with an asterix, which removes the selected ID from the list, causes ng-change to be fired again with a null value as the new ID.
但是,标有星号的行会从列表中删除选定的ID,这会导致以空值作为新ID再次触发ng-change。
I am struggling to find a solution to work around this. 我正在努力寻找解决方案来解决此问题。
The same code works fine in AngularJS 1.2, but after moving to 1.7 I am seeing this issue. 相同的代码在AngularJS 1.2中可以正常工作,但是在移至1.7之后,我看到了这个问题。
$scope.updateSaleItemIds = function(saleItem, newid) {
var ind = $scope.availableSaleItemIds.indexOf(newid);
if (!isNaN(saleItem.id)) {
*$scope.availableSaleItemIds[ind] = saleItem.id;*
}
else {
$scope.availableSaleItemIds.splice(ind,1);
}
$scope.availableSaleItemIds.sort(function(a, b){return a - b});
var indE = $scope.saleItems.indexOf(saleItem);
$scope.saleItems[indE].id = newid;
};
HTML HTML
<tr ng-repeat="saleItem in saleItems" class="createForm">
<select ng-model="selectedId"
ng-options="id for id in availableSaleItemIds"
ng-change="updateSaleItemIds(saleItem, selectedId)">
<option value="">{{saleItem.id}}</option>
</select>
</tr>
AngularJS V1.4 included a major refactoring of the ng-options
directive. AngularJS V1.4包括
ng-options
指令的主要重构。 The default option is now selected with a model value of null
. 现在选择默认选项,其模型值为
null
。 For more information, see AngularJS Developer Guide - Migrating to V1.4 - ngOptions . 有关更多信息,请参阅《 AngularJS开发人员指南-迁移至V1.4-ngOptions》 。
When the user selects an option, the model is set to that new value and the ng-change
updates the available options. 当用户选择一个选项时,模型将设置为该新值,并且
ng-change
更新可用选项。 The updateSaleItemsIds
function removes that value from the availableSaleItemIds
list. updateSaleItemsIds
函数从availableSaleItemIds
列表中删除该值。 When the new list of options does not include the model value, the ng-options
directive sets the model back to null
and invokes the ng-change
directive again. 当新的选项列表不包括模型值时,
ng-options
指令将模型设置回null
并再次调用ng-change
指令。
The work around is to not update the list of options when the selected value is changed to null
. 解决方法是,当所选值更改为
null
时,不更新选项列表。
<select ng-model="selectedId"
ng-options="id for id in availableSaleItemIds"
̶n̶g̶-̶c̶h̶a̶n̶g̶e̶=̶"̶u̶p̶d̶a̶t̶e̶S̶a̶l̶e̶I̶t̶e̶m̶I̶d̶s̶(̶s̶a̶l̶e̶I̶t̶e̶m̶,̶ ̶s̶e̶l̶e̶c̶t̶e̶d̶I̶d̶)̶"̶
ng-change="selectedId && updateSaleItemIds(saleItem, selectedId)">
<option value="">{{saleItem.id}}</option>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.