srv srv - 1 year ago 92
AngularJS Question

By default Selecting dropdown value conditionally

I have a dropdown with 2 options

Month & Year
. Based on API call response, If
period == Y
, Year should be selected by default & if
period == M
, Month should be selected.
I dont know how to do this.

<div class="col-sm-3 PDL dateY">
<select class="form-control" id="sel1" ng-model="SelectedValue_free" ng-options="details.period for details in validity_dd">

$scope.validity_dd = [
period: 'Year'
}, {
period: 'Month'

Answer Source

Based on your condition after you get the information from the api, set

 if(period == 'Y') 
 $scope.SelectedValue_free= 'Year';
 else if(period == 'M') 
 $scope.SelectedValue_free= 'Month';

Change you ng-options to ,

 ng-options="details.period as details.period for details in validity_dd"

OR you can set

 if(period == 'Y') 
 $scope.SelectedValue_free= $scope.validity_dd[0];
 else if(period == 'M') 
 $scope.SelectedValue_free= $scope.validity_dd[1];

ng-options="details.period for details in validity_dd"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download