天天看点

angularJs操作select列表框

ng-options="v.value as v.name for v in data"   
//v.value相当于option的value值
//v.name为option填充显示的值
//v in data:data遍历,v为每个遍历值别名
ng-model="city"
//city值为选中的value的值
           
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="angular.min.js"></script>
</head>
<body>
<div ng-app="module" ng-controller="ctrl">
    <select ng-options="v.value as v.name for v in data" ng-model="city">
        <option value="">请选择城市</option>
    </select>
    {{city}}
</div>
<script>
    var m = angular.module('module', []);
    m.controller('ctrl', ['$scope', function ($scope) {
        $scope.city = 'beijing';//默认选中北京
        $scope.data = [
            {name: '上海', value: 'shanghai'},
            {name: '北京', value: 'beijing'},
            {name: '南京', value: 'nanjing'},
        ];
    }]);
</script>
</body>
</html>
           

继续阅读