AngularJS 选择框

1.ng-repeat和ng-options的区别

    ng-repeat 循环的是数组,ng-options 可以是对象和数组;

    ng-options 有分组和排序功能;

举栗子:

    1. 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 = ["Google", "Runoob", "Taobao"];
    });
</script>
</body>

 2. ng-options:

    

<body>
<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>
</body>


3. ng-options 分组

<body>
<div ng-app="myApp" ng-controller="myCtrl">
    <select ng-model="colorChosen" ng-options="color.name group by color.type for color in colors">
    </select>
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function ($scope) {
        $scope.colors = [
            {name: '黑色', color: 'black', type: "暗色"},
            {name: '白色', color: 'white', type: "亮色"},
            {name: '红色', color: 'red', type: "暗色"},
            {name: '蓝色', color: 'blue', type: "暗色"},
            {name: '黄色', color: 'yellow', type: "亮色"}
        ];
    });
</script>
</body>

4. ng-options 排序

<div ng-app="myApp" ng-controller="myCtrl">
    <select ng-model="colorChosen" ng-options="color.name group by color.type for color in colors | orderBy:'name' ">
    </select>
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function ($scope) {
        $scope.colors = [
            {name: '黑色', color: 'black', type: "暗色"},
            {name: '白色', color: 'white', type: "亮色"},
            {name: '红色', color: 'red', type: "暗色"},
            {name: '蓝色', color: 'blue', type: "暗色"},
            {name: '黄色', color: 'yellow', type: "亮色"}
        ];
    });
</script>
</body>

5. ng-options 禁用

<div ng-app="myApp" ng-controller="myCtrl">
    <select ng-model="colorChosen"
            ng-options="color.name group by color.type
        disable when color.disabled for color in colors"></select>
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function ($scope) {
        $scope.colors = [
            {name: '黑色', color: 'black', type: "暗色"},
            {name: '白色', color: 'white', type: "亮色", disabled: false},
            {name: '红色', color: 'red', type: "暗色", disabled: true},
            {name: '蓝色', color: 'blue', type: "暗色", disabled: false},
            {name: '黄色', color: 'yellow', type: "亮色", disabled: true}
        ];
    });
</script>
</body>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值