zoukankan      html  css  js  c++  java
  • angular实时显示checkbox被选中的元素

    /**
     * Created by zh on 20/05/15.
     */
    // Code goes here
    
    var iApp = angular.module("App", []);
    
    
    
    iApp.controller('AddStyleCtrl', function($scope)
    {
        $scope.tagcategories = [
            {
                id: 1,
                name: 'Color',
                tags: [
                    {
                        id:1,
                        name:'color1'
                    },
                    {
                        id:2,
                        name:'color2'
                    },
                    {
                        id:3,
                        name:'color3'
                    },
                    {
                        id:4,
                        name:'color4'
                    },
                ]
            },
            {
                id:2,
                name:'Cat',
                tags:[
                    {
                        id:5,
                        name:'cat1'
                    },
                    {
                        id:6,
                        name:'cat2'
                    },
                ]
            },
            {
                id:3,
                name:'Scenario',
                tags:[
                    {
                        id:7,
                        name:'Home'
                    },
                    {
                        id:8,
                        name:'Work'
                    },
                ]
            }
        ];
    //用于存储选中的值
        $scope.selected = [];
        $scope.selectedTags = [];
    //更新事件
        var updateSelected = function(action,id,name){
            if(action == 'add' && $scope.selected.indexOf(id) == -1){
                $scope.selected.push(id);
                $scope.selectedTags.push(name);
            }
            if(action == 'remove' && $scope.selected.indexOf(id)!=-1){
                var idx = $scope.selected.indexOf(id);
                $scope.selected.splice(idx,1);
                $scope.selectedTags.splice(idx,1);
            }
        }
    //用于监控点击事件,checkbox选择了就更新
        $scope.updateSelection = function($event, id){
            var checkbox = $event.target;
            var action = (checkbox.checked?'add':'remove');
            updateSelected(action,id,checkbox.name);
        }
    //判断存储的变量中是否已包含该checkbox,$scope.selected.indexOf(id)>=0是个布尔值
    $scope.isSelected = function(id){ return $scope.selected.indexOf(id)>=0; } });
    <!DOCTYPE html>
    <html data-ng-app="App">
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
        <script src="script2.js"></script>
    </head>
    <body data-ng-controller="AddStyleCtrl">
    
        <div>Choose Tags</div>    
        <div>
            <div>You have choosen:</div>
            <hr>
            <label data-ng-repeat="selectedTag in selectedTags">
                (({{selectedTag}}))
            </label>
            <hr>
            <div data-ng-repeat="category in tagcategories">
                <div>{{ category.name }}</div>
                <div data-ng-repeat="tag in category.tags">
                    <div>
                <!--根据ng-repeat生成一些button--> <input type="checkbox" id={{tag.id}} name="{{tag.name}}" ng-checked="isSelected(tag.id)" ng-click="updateSelection($event,tag.id)"> {{ tag.name }} </div> </div> <hr> </div> </div> <pre>{{selected|json}}</pre> <pre>{{selectedTags|json}}</pre> </body> </html>

      出处: http://www.cnblogs.com/CheeseZH/ 

  • 相关阅读:
    【摄影】延时摄影
    【sas sql proc】统计
    【分析模板】excel or sas
    JavaScript的方法和技巧
    好书推荐
    七招制胜ASP.NET应用程序开发
    .Net中使用带返回值的存储过程(VB代码)
    ASP.NET 2.0构建动态导航的Web应用程序(TreeView和Menu )
    简单查询和联合查询两方面介绍SQL查询语句
    数字金额大小写转换之存储过程
  • 原文地址:https://www.cnblogs.com/zipon/p/5825973.html
Copyright © 2011-2022 走看看