How to set a preselected option as default in a select field using AngularJS

后端 未结 2 580
死守一世寂寞
死守一世寂寞 2021-01-28 19:16

I am having troubles with setting the default value of a select box in HTML using AngularJS.

Here\'s my scope variable in the controller:

$scope.sensorRe         


        
相关标签:
2条回答
  • 2021-01-28 19:39

    Have you tried using ng-options instead of creating separate option elements?

    <select id="itemsPerPage" name="itemsPerPage" class="panel panel-default" ng-options="item in items track by $index" ng-model="sensorReadingsPerPage" ng-change="changeItemsPerPage()">
    

    And in your controller

    $scope.items = [10, 20, 30, 40, 50, 100];
    $scope.sensorReadingsPerPage = $scope.items[2];
    
    0 讨论(0)
  • 2021-01-28 19:47

    Try setting $scope.sensorReadingsPerPage = "30" in your controller.

    Note: It should be a string.

    0 讨论(0)
提交回复
热议问题