【发布时间】:2021-10-28 08:41:55
【问题描述】:
请帮我解决我的问题我对angularjs有点新,我的问题是我需要能够在只有一个项目时在选择选项中设置默认值,因为它是一个动态选择选项,它有另一个包含许多项目的下拉列表,但没关系,当必须使用 ng-options 在选择选项中选择一个项目时需要什么
<select class="form-control form" ng-model="relatedTo" style="height: 40px;" ng-options="c.CUSTCODE as c.CUSTNAME + ' - ' + c.CUSTCODE for c in customers | filter:code" > </select><span style="color:red" ng-show="type === 9 || type !== 9"></span>
ANGULARJS
$http.post('commandCenter.aspx/allCustomer', {}).then(
function onSuccess(response) {
$scope.customers = JSON.parse(response.data.d);
console.log($scope.customers); },
function onError(response) {
console.log('error !!! ');
});
图一 这张图没问题,因为他有很多物品清单。
【问题讨论】:
-
您可以在您的函数中检查选项数组的长度是否为 1。如果是这样,则将模型值设置为数组的第一项。您可以做的其他选择是在您的 html 中使用 ng-init 并始终选择数组的第一项,因此它可以是您的默认选择
-
@MrJami I've already done this but what really needs to be done when there's only one item in the select should be automatically selected.
标签: javascript angularjs