如何在选择列表中选择选项?

时间:2015-06-16 06:51:46

标签: angularjs dropdownbox

我在select中有选项元素:

$scope.countries = {0 : 'Select country...'};

ng-model="selected"是整数0;

但未选择选项。如何用0选择选项?

2 个答案:

答案 0 :(得分:0)

我不确定我明白你要做什么...... 但我想你想在ngModel中选择值0时显示文本...所以你想让ngOptions按值去显示文本吗?

所以你想要这样的东西:

ng-options =“ country as country.id for country by country by country.id” 请检查....所以你实际上可以做类似

的事情
<select ng-options="item.subItem as item.label for item in values track by item.id" ng-model="selected">


    $scope.values = [{
  id: 1,
  label: 'aLabel',
  subItem: { name: 'aSubItem' }
}, {
  id: 2,
  label: 'bLabel',
  subItem: { name: 'bSubItem' }
}];

$scope.selected = { name: 'aSubItem' };

请检查 ngOptions docs

答案 1 :(得分:0)

如果我找对你,那么你正在寻找类似这样的东西

<html>
<body ng-app="myApp" ng-controller="HomeCtrl">

    <select ng-model="selected" ng-options="v for (k,v) in country ">
    </select>

 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <script>

    var app=angular.module('myApp', []);

    app.controller('HomeCtrl', ['$scope',  function($scope) {

        $scope.selected='Select country...';  // dafault to be displayed

        $scope.country={0 : 'Select country...', 1:'France', 2:'India', 3:'USA'};

    }]);

  </script>
</body>
</html>

注意: - 对象的键始终必须是字符串数据类型。您不能使用整数或数字,只会被视为字符串。