gpt4 book ai didi

javascript - 额外选项和 ng-required

转载 作者:行者123 更新时间:2023-12-03 11:48:27 28 4
gpt4 key购买 nike

我使用以下代码向我的选项列表添加一个额外的选项:

var defaultOption = { id: 0, descrip: 'Select Item Tree...' };
$scope.metaData.itemTrees.splice(0, 0, defaultOption);

现在,在某些情况下我希望需要项目树,所以我有

<div class="col-lg-9">
<select class="form-control" name="itemTree" id="itemTree"
ng-model="currentSalespoint.iTreeTop" ng-required="salespointMode.needsItemTree"
ng-options="t.id as t.descrip for t in metaData.itemTrees"></select>
</div>
<div class="field-validation-error">
<span ng-show="editSalespointGeneral.itemTree.$error.required && editSalespointGeneral.itemTree.$dirty">Item Tree selection is required.</span>
</div>

问题是 0 不应该是一个有效的选择,例如我无法将“选择项目树”保留为选定选项。我应该怎么做才能真正需要它?

如果我尝试使用 null 而不是 0,则会收到错误,但随后“Select ..”消失,下拉列表中出现空行。

最佳答案

解决方案之一是直接在html中定义空项目:

<select class="form-control" name="itemTree" id="itemTree"
ng-model="currentSalespoint.iTreeTop" ng-required="salespointMode.needsItemTree"
ng-options="t.id as t.descrip for t in metaData.itemTrees">
<option value="">Select Item Tree...</option>
</select>

骗子:http://plnkr.co/edit/7m8YYVz7HNs1OY7hRIF1?p=preview

关于javascript - 额外选项和 ng-required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25943406/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com