简体   繁体   English

默认情况下,有条件地选择下拉值

[英]By default Selecting dropdown value conditionally

I have a dropdown with 2 options Month & Year . 我有一个下拉菜单,其中有2个选项Month & Year Based on API call response, If period == Y , Year should be selected by default & if period == M , Month should be selected. 基于API调用响应,如果period == Y ,则默认选择Year;如果period == M ,则选择Month。 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">
    </select>
</div>    

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

Based on your condition after you get the information from the api, set 从api获取信息后,根据您的情况进行设置

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

Change you ng-options to , 将您的ng-options更改为,

 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"

Use the following script in app.js 在app.js中使用以下脚本

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

//this is an array to store all the possible options as key value pairing
$scope.opt_arr={'Y':'Year','M':'Month'};

//Here you can set the value of period with api call response data
$scope.period='M';

//this will search the object from the array which match the data
var result = $.grep($scope.validity_dd, function(e)
            { return e.period == $scope.opt_arr[$scope.period]; });

//this will set the model value so that the required option will be selected
$scope.SelectedValue_free=result[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM