当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > Angular.JS判断复选框checkbox是否选中并实时显示

Angular.JS判断复选框checkbox是否选中并实时显示

2018年05月08日  | 移动技术网IT编程  | 我要评论

首先来看看简单的效果图,如下所示:

看一下html代码:

<!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>
     <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>

line2 定义了angularjs app;

line4 引入angularjs脚本;

line5 引入自己写的script2.js脚本;

line7 指定控制器addstylectrl

line13-15 实时显示已选标签给用户看;

line17-line26 使用双重循环列出数据库(本例中就存储在了controller的一个对象里)中的数据;

  line21的这行代码作用可大了:<input type="checkbox" id={{tag.id}} name="{{tag.name}}" ng-checked="isselected(tag.id)" ng-click="updateselection($event,tag.id)">

  存储了tag的id,name,利用isselected(tag.id)来判断是否被checked,点击时候调用updateselection($event,tag.id)方法;

  如果想 ng-click 触发的函数里获取到该触发该函数的元素不能直接传入 this ,而需要传入 event。因为在angularjs里面,这个地方的this是event。因为在angularjs里面,这个地方的this是scope 。我们可以传入 event,然后在函数里面通过event,然后在函数里面通过event.target 来获取到该元素。

line29-30 是测试时候给自己看的,可以看到selected数组和selectedtags数组中的内容;

然后看看angularjs代码:(script2.js)

/**
 * 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);
  }
 }

 $scope.updateselection = function($event, id){
  var checkbox = $event.target;
  var action = (checkbox.checked?'add':'remove');
  updateselected(action,id,checkbox.name);
 }

 $scope.isselected = function(id){
  return $scope.selected.indexof(id)>=0;
 }
});

line6 定义了angular app;

line10 定义了控制器addstylectrl;

line12-63 定义了 标签对象

line64,66 声明了$scope中的两个数组对象(可以合并为1个),分别用来存储tag的id和name;

line68-78 定义了updateselected方法,这个方法会被updateselection调用;

  line69-72:如果add操作且 ‘数组[id]' 元素不存在,向数组中添加数据(id,name);

  line73-77:如果remove操作且‘数组[id]' 元素存在,从数组中删除数据(id,name);

line80-84定义了updateselection方法,这个方法会在html页面的checkbox被点击时调用;

  line81通过$event变量来获取点击的dom元素;

  line82通过checkbox的当前状态来决定是add操作还是remove操作;

  line83调用updateselected方法,更新数据;

line86-88定义了isselected方法,用来判断id为id的checkbox是否被选中,然后传值给页面的ng-checked指令;

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问欢迎大家留言交流。

作者: zh奶酪——张贺
q q: 1203456195
邮箱:
出处:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网