AngularJS Select(选择框)

ng-options 创建下拉选择框

<div ng-app="myApp" ng-controller="myCtrl">
<select ng-init="selectedName = names[0]" ng-model="selectedName" ng-options="x for x in names">
</select>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.names = ["Google", "Runoob", "Taobao"];
});
</script>

ng-repeat 创建下拉选择框

<body>
<div ng-app="myApp" ng-controller="myCtrl">
    <select>
        <option ng-repeat="x in names">{{x}}</option>
    </select>
</div>
<script>
    var app=angular.module('myApp',[]);
    app.controller("myCtrl",function ($scope) {
        $scope.names=["111","2222","3333"]
    })
</script>
</body>

1. ng-options:选择的值是一个对象。用法:ng-options="x.site for x in sites"
2. ng-repeat:选择的值是一个字符串。用法:ng-repeat="x in sites"

ng-options 实现键值对

<div ng-app="myApp" ng-controller="myCtrl">
    <p>选择一辆车:</p>
    <select ng-model="selectedCar" ng-options="x for (x, y) in cars">
    </select>
    <h1>你选择的是: {{selectedCar.brand}}</h1>
    <h2>模型: {{selectedCar.model}}</h2>
    <h3>颜色: {{selectedCar.color}}</h3>
    <p>注意选中的值是一个对象。</p>
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.cars = {
            car01 : {brand : "Ford", model : "Mustang", color : "red"},
            car02 : {brand : "Fiat", model : "500", color : "white"},
            car03 : {brand : "Volvo", model : "XC90", color : "black"}
        }
    });
</script>
</body>

下拉菜单也可以换成value的值

<select ng-model="selectedCar" ng-options="y.brand for (x, y) in cars">

你可能感兴趣的:(AngularJS,javascript)