3

How i select auto select value if option is only one

<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>

My Problem is if i have multiple option it ask to select option if it have only one it needs to automatically select and dispaly the value how i fix this

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Develop
  • 75
  • 2
  • 12
  • you can use `ng-init` directive for this. call a function in `ng-init` and check size of `institutions`. if that size be one init ur model else don't init. – Hadi J Mar 10 '17 at 07:10
  • can you please tell me how to use in my code – Develop Mar 10 '17 at 07:14

1 Answers1

0

Try like this.

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>
Hadi J
  • 16,989
  • 4
  • 36
  • 62