【问题标题】:how to check up checkbox from another controller如何从另一个控制器检查复选框
【发布时间】:2014-08-11 16:52:40
【问题描述】:

我有一个复选框列表,需要计算选中的复选框。

<div id="list" ng-controller = "listContr as ilist">
  <ul>
    <li ng-repeat = "item in ilist.items| filter: searchText">
      <input type = "checkbox" ng-model="item.done" id="ch">
    </li>
  </ul>
</div>

在我的项目中,我尝试使用另一个控制器来获取此值。这也很复杂,因为这个值应该是动态改变的。

【问题讨论】:

  • 我要使用这个值来绘制图表,所以我需要一直重写它,复选框被选中/取消选中。

标签: javascript angularjs checkbox


【解决方案1】:

请看这里http://jsbin.com/guzaco/2/edit

您可以使用角度服务在控制器之间共享数据。

JS:

var app = angular.module('app', []);

app.service("dataService", function(){

    var _items = [    
      {id:1, done:false},
      {id:3, done:true},
      {id:2, done:false}

  ];



  return {

    items:_items,


  };
});
app.controller('firstCtrl', function($scope,$filter,dataService){
 $scope.items = dataService.items;

  $scope.$watch('items', function(){

    $scope.count = ($filter('filter')($scope.items, {done:true})).length;

  }, true);




});

app.controller('listContr', function($scope,dataService){

 $scope.data = dataService;
});

HTML:

<body ng-app="app">
  <div ng-controller="firstCtrl">
    <p>First Controller</p>
     Count: {{count}}

      </div>

  <div id="list" ng-controller = "listContr">
      <p>List Controller</p>

  <ul>
    <li ng-repeat = "item in data.items| filter: searchText">
      <input type = "checkbox" ng-model="item.done" id="ch">
    </li>
  </ul>


</div>
</body>

【讨论】:

    【解决方案2】:
    <script>
    
    $(document).ready(function()
                        {
                           //Method 1:
    
                           alert($('.checkbox_class_here :checked').size());
    
                           //Method 2:
    
                           alert($('input[name=checkbox_name]').attr('checked'));
    
                           //Method: 3
    
                           alert($(":checkbox:checked").length);
    
    
                        });
    
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多