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

  • 相关阅读:
    xshell,putty远程连接Linux并使用密钥认证
    配置IP
    认识linux
    dpkg: error processing package libraspberrypi-doc (--configure): package is in a very bad inconsistent state;
    pi4j,Netbeans中togglebutton跟Jbutton的区别
    Netbeans配置Java SE嵌入式平台(树莓派)
    发光二极管正负极判断
    Eclipse安装图形JFrame,Jswing编程界面
    阿里云Centos6.9安装图形化界面
    Oracle 一个中文汉字 占用几个字节,由Oracle中字符集编码决定
  • 原文地址:https://www.cnblogs.com/zipon/p/5825973.html
Copyright © 2020-2023  润新知