Angular checklist-model checkboxes with reverse action

不羁的心 提交于 2019-12-25 04:42:30

问题


I'm working with checklist-model.js for angular to select from dynamically generated list of objects. It's working fine, but now I need to make it work in reverse so when I uncheck any of checkbox - place it to new array (and when checked back - remove from array). Can any of You give me some ideas or tell me next steps how to deal with it?

html:

<label>
    <input type="checkbox"
        ng-model="check_all_domains"
        ng-click="toggle_select_all()"/> all
</label>
<label ng-repeat="objects in objects_model">
    <input type="checkbox"
        checklist-model="objects_selected"
        checklist-value="objects"
        ng-checked="check_all_domains"/>
    {{objects.name}}
</label>

model:

$scope.objects_model = [
    {id : '1', name: 'name1'},
    {id : '2', name: 'name2'},
    {id : '3', name: 'name3'},
  ];
$scope.objects_selected = [];
$scope.check_all_domains = false;

$scope.toggle_select_all = function() {
    $scope.objects_selected = [];
};

here is screenshot how it's working right now:

and here is how I want it to work:

UPDATED: WORKING AS IT SHOULD DEMO


回答1:


I had a similar issue with checklist-model and I've managed to create a workaround

Although its a pretty nasty solution, it works:

$scope.toggle_select_all = function() {
   $timeout(function() {
       $scope.check_all_domains = $scope.check_all_domains ? false : true;
   });

   if (!$scope.check_all_domains) {
      angular.copy($scope.objects_model, $scope.objects_selected);
   } else {
      angular.copy([], $scope.objects_selected);
   }
};

See this plunkr: http://plnkr.co/edit/CiXO1debaDkKPHPYKNfT?p=preview


I'd highly suggest searching for an alternative, cause later on you'll see that it pays off.

For me this worked: http://jsfiddle.net/cjwprostar/M4vGj/6/ - Chris Waguespack

Source: https://groups.google.com/forum/#!topic/angular/KMS5hXn1OCI




回答2:


Updated my demo in question so have a look at the final result

<label ng-repeat="objects in objects_model" class="test">
    <input type="checkbox" 
          checklist-model="objects_selected" 
          checklist-value="objects" />
    <i ng-class="{checked : check_all_domains, 
                unchecked : !check_all_domains, 
                fakecheck : check_all_domains}"></i>{{objects.name}}
  </label>


来源:https://stackoverflow.com/questions/30866463/angular-checklist-model-checkboxes-with-reverse-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!