代码之家  ›  专栏  ›  技术社区  ›  Niranjan Godbole

如何在angularjs中将默认值设置为dropdownlist

  •  0
  • Niranjan Godbole  · 技术社区  · 8 年前

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

    Dropdown values

    如果我想在默认情况下设置第四个值,那么我尝试如下。

    $scope.period = $scope.cal[4]; and $scope.period = response.data.data.Period[4];

    以上两种选择都不适合我。我可以知道设置默认值的解决方案吗?任何帮助都将不胜感激。非常感谢。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Vivek Atri    8 年前

    $scope.calValue = $scope.cal[3]; //This will be Default cal value
    

    <select ng-model="calValue" ng-options="x.Period for x in cal"></select>
    

    现在,calValue将是下拉列表中的默认值,如果您从下拉列表中选择任何其他选项,则该值将成为calValue的值。

    干杯

        2
  •  1
  •   Sajeetharan    8 年前

    如果要设置第四个值索引,则应为3

     $scope.period = $scope.cal[3]; 
     $scope.period = response.data.data.Period[3];