2016-09-07 1 views
0

두 개의 옵션 Month & Year이있는 드롭 다운 목록이 있습니다. API 호출 응답을 기반으로 period == Y 인 경우 인 경우 연도를 기본값으로 &으로 선택해야합니다. 월을 선택해야합니다. 나는 이것을 어떻게하는지 모른다. 당신은 API에서 정보를 얻을 후 조건에 따라기본적으로 드롭 다운 값을 조건부로 선택

<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' 
    } 
] 
+0

드롭 다운 목록에 null 값이 있습니까? – VjyV

+0

ng-model 변수 때문에 js 파일에서 선언되지 않으므로 null이 발생합니다. – VjyV

답변

0

, 당신이-옵션을 겨

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

변경,

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

을 설정 또는 당신은

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" 
을 설정할 수 있습니다
+0

드롭 다운 옵션에 빈 옵션이 있습니다. 값을 선택하지 않습니다. – srv

+0

ng-option 표현식 변경 시도 – Hmahwish

0

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