gpt4 book ai didi

angularjs - angularjs中的ng自动选择值

转载 作者:行者123 更新时间:2023-12-03 08:02:18 24 4
gpt4 key购买 nike

如果选项只有一个,我如何选择自动选择值

<select class="perf-select" ng-model="viewProfileCtrl.neft.institution"
ng-options="inst.institution.institution_id as inst.institution.name for inst in viewProfileCtrl.perfiosAnalysisData.institutions"
ng-change="viewProfileCtrl.setCurrNeftInsti(viewProfileCtrl.neft.institution, 'neftCredit')">
<option value="" selected>Select a Bank</option>
</select>

我的问题是如果我有多个选项它会要求选择选项如果它只有一个它需要自动选择并显示值我如何解决这个问题

最佳答案

像这样尝试。

var app = angular.module('anApp', []);
app.controller('aCtrl', function($scope) {

$scope.chooses = [{"key":"one","value":1},{"key":"Two","value":2},{"key":"Three","value":3}];

$scope.chooses2 = [{"key":"one","value":1}];
$scope.setDefault = function(){
if($scope.chooses.length ==1)
$scope.model = $scope.chooses[0].value;
}

$scope.setDefault2 = function(){
if($scope.chooses2.length ==1)
$scope.model2 = $scope.chooses2[0].value;
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="anApp" ng-controller="aCtrl">
<select ng-init="setDefault()" ng-model="model" ng-options="k.value as k.key for k in chooses">
</select>

<select ng-init="setDefault2()" ng-model="model2" ng-options="k.value as k.key for k in chooses2">
</select>
</div>

关于angularjs - angularjs中的ng自动选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42712288/

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