我想要一个选择框,从中可以选择带有最小/最大数字的列表。

目前我的电话号码是1到10,所以我有以下内容。

<body ng-app="demoApp">
    <div ng-controller="DemoController">
            <select ng-model="selectedItem"
                ng-options="opt as opt for opt in options">
            </select>
            The value selected is {{ selectedItem }}.
    </div>
</body>
angular.module('demoApp', []).controller('DemoController', function($scope) {
  $scope.options = [1,2,3,4,5,6,7,8,9,10];
  $scope.selectedItem = $scope.options[1];
});


最好的方法是什么?例如,如果我想从1到100之间的数字中进行选择,则我不想只列出最低和最高数字。
使用Vanilla JS时,我在想以下类似的事情,但是在这里寻找一种更具角度的方法,以便可以轻松地使用ng-model更新我的数据。

var selectList = '<select>';
for (var x = 0; x < 100; x++) {
      selectList += "<option value="+ x +">" + x + "</option>";
}
selectList += '</select>';

最佳答案

angular.module('demoApp', []).controller('DemoController', function($scope) {
  $scope.options = [];

  //Fill array with incremental numbers
  while ($scope.options.length < 100){
    $scope.options.push($scope.options.length + 1);
  }

  $scope.selectedItem = $scope.options[1];
});

关于javascript - AngularJS将增量数字添加到选择列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28489619/

10-09 21:10