[英]How to set default value to dropdownlist in angularjs
嗨,我正在用angularjs開發Web應用程序。 我有一個帶有某些值的dropdownlistbox。 以下是我的dropdownlistbox html代碼。
<select id="calc-year" ng-model="period" name="period" required range-numberddwn="rangeddwn">
<option value="" label="{{ 'Month' | translate }}">{{ 'Month' | translate }}</option>
<option ng-repeat="x in cal" value="{{x.Period}}">{{x.Period}} {{'Months' | translate}}</option>
</select>
下面是我的代碼,將值分配給dropdownlistbox。
$scope.cal = response.data.data.Period;
以下是我分配給cal的數據的屏幕截圖。
如果我想默認設置第四值,那么我嘗試如下。
$scope.period = $scope.cal[4]; and $scope.period = response.data.data.Period[4];
每當我設置為上述值時,我的空白值就會出現在下拉列表中
以上兩種選擇對我來說都不可行。 我可以知道設置默認值的解決方案嗎? 任何幫助,將不勝感激。 謝謝。
如果要設置第四個值索引應為3
$scope.period = $scope.cal[3];
$scope.period = response.data.data.Period[3];
在您的控制器中,在作用域變量中設置一個值,該值要用作下拉菜單的默認值。 確保范圍內存在cal變量。 然后嘗試這樣的事情,
$scope.calValue = $scope.cal[3]; //This will be Default cal value
然后在您的view(html)中使用ng-options這樣的東西,
<select ng-model="calValue" ng-options="x.Period for x in cal"></select>
現在,calValue將是下拉菜單中的默認值,如果您從下拉列表中選擇任何其他選項,則它將成為calValue的值。
干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.