我这辈子都想不通。使用 AngularJS。
我有一个带有多个选项的下拉选择字段。它是可以多次完成的表单的一部分(即“添加另一个”类型的表单)。现在,其中一个选项只能使用一次。使用该选项后,如何从所有其他选择字段中删除该选项?
我在做什么:
html
<select ng-model="item.itemtype">
<option ng-repeat="i in itemtype" value="{{i}}" ng-init="item.itemtype = itemtype[0]">{{i}}</option>
</select>
Angular
$scope.Items = [
{ 'itemtype': '', 'desc': '', 'color': '' }
];
$scope.itemtype = [
'shirt',
'pants',
'hats',
'shoes',
'special'];
我尝试过的(但确实行不通)
$scope.specialremove = function() {
var exist = Items.indexOf("special")
if (exist !== 0) {
return '';
}
else {
return 'special';
}
}
我希望我不必求助于任何其他框架来解决这个问题。请随时指出我的代码中的任何其他问题/错误/效率低下。
最佳答案
可以帮助的第一件事是使用 ng-options
:
ng-options="type for type in itemType"
.
拥有 label
的对象会更好和 value
属性,以便将其写为
ng-options="type.value as type.label for type in itemType"
并将显示的标签与选择的实际值分开。
在你的 Controller 中你应该有这样的东西:
$scope.itemType= [
...
];
$scope.selectedItem= $scope.itemType[0];
这样您就可以将选择写成:
<select ng-Model="selectedItem" ng-options="item.value as item.label for item in itemType"></select>
要删除所选项目,您可以 watch
selectedItem
的值在 Controller 中:当它与您想要的值匹配时,将其从数组中删除并更新 selectedItem
相应地。
这是一个基本的 fiddle .我只是在选择第三个选项后将其删除,并将所选项目重置为第一个。
关于javascript - angularjs - 从输入选择中删除选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23882245/