V
V
Vyacheslav Popov2015-02-14 14:12:11
Angular
Vyacheslav Popov, 2015-02-14 14:12:11

How to disable all checkboxes in AngularJS?

There are li tags , they contain inputs with ng-checked attributes with valueReset variables , a checkbox reset button with the ng-model="valueReset" attribute .
In the controller, for the reset button, I set the valueReset variable to false

$scope.resetFilter = function () {
  $scope.valueReset = false;
}

<button ng-model="valueReset" ng-click='resetFilter()'> отчистить фильтр</button>

<li>
    <input type="checkbox" ng-checked="valueReset" id="check_tag" />
    <label for='check_tag'> TAG Heuer</label>
</li>
 <li>
    <input type="checkbox" ng-checked="valueReset" id="check_iwc" />
    <label for='check_iwc'> IWC</label>
</li>
<li>
    <input type="checkbox" ng-checked="valueReset" id="check_rado" />
    <label for='check_rado'> RADO</label>
</li>

when you click on the button, clears the first time, then ignores - you need to reload the page again. How to disable checkboxes correctly? Maybe you need to include the ngChange, ngValue attributes, etc.?
Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav Popov, 2015-02-14
@VyacheslavPopov

Controller

watchesStoreControllers.controller('HomeCtrl', ['$scope',  'Watch',

  function($scope, Watch) {

// данные
    $scope.watches = Watch.query();

//  вывод выбранных критериев из фильтра(список  того что выбрали в фильтре)
    $scope.resultIncludes = [];

//  определил для каждого чекбокса переменную в ng-model , но при нажатии
//  на чекбокс - false на true не меняет, 
//  я должен это сделать сам в контроллере после события в чекбоксе?
    $scope.tag = false;
    $scope.zen = false;
    $scope.iwc = false;

 // функция для вывода  выбранных критериев из фильтра
    $scope.includeBrand = function(brand) {
        var i = $.inArray(brand, $scope.resultIncludes);
        if (i > -1) {
          $scope.resultIncludes.splice(i, 1);
        } else {
          $scope.resultIncludes.push(brand);
        }
    }
                
// отфильтровывает и выводит часы на страницу
    $scope.checkboxFilter = function(watches) {
      if ($scope.resultIncludes.length > 0) {
        if ($.inArray(watches.brand, $scope.resultIncludes) < 0)
          return;
        }
        return watches;
    }

// проверяет, если хоть один критерий выбран, удаляет только список критериев,
// хочу добавить здесь отключение всех чекбоксов
    $scope.resetFilter = function () {
      if($scope.resultIncludes[0]) {
        $scope.resultIncludes.splice(0, $scope.resultIncludes.length);
      }
    }
  }
]);

In HTML (for short, the jade extension) checkboxes and the output of hours (products)
<--! фильтр-->
ul
  li
    input(type="checkbox" id="check_tag" ng-model='tag' class="css-checkbox")
    label(for='check_tag' class="css-label") TAG Heuer
  li
    input(type="checkbox" id="check_zenith" ng-model='zen' class="css-checkbox")
    label(for='check_zenith' class="css-label") ZENITH
  li
    input(type="checkbox" id="check_iwc" ng-model='iwc' class="css-checkbox")
    label(for='check_iwc' class="css-label") IWC


// отключаем чекбоксы и стираем выбранные критерии поиска
button( ng-click='resetFilter()')


<--! список товаров-->
ul
  li(ng-repeat="watch in watches | filter:q | filter:checkboxFilter")
    a(ng-href="/{{watch.id}}")
      img(ng-src="{{watch.imageUrl}}", alt="{{watch.name}}", class="images")

// массив данных
[
  {
    "id": "Monaco-Twenty-Four-McQueen",
    "imageUrl": "img/watches/CAL5111.FC6299.jpg",
    "brand":"TAG Heuer",
    "name":"Monaco Twenty Four McQueen",
    "mech":"Calibre 36, механика с автоподзаводом"
  },

  {
    "id": "Aquaracer-500-M-Calibre-5-Day-Date",
    "imageUrl": "img/watches/WAJ2180.FT6015.jpg",
    "brand":"LONGINES",
    "name":"Aquaracer 500 M Calibre 5 Day-Date",
    "mech":"Calibre 5 Day-Date, механика с автоподзаводом"
  },

  {
    "id": "Aquaracer-Calibre-16-Day-Date",
    "imageUrl": "img/watches/CAF5011.BA0815.jpg",
    "brand":"TAG Heuer",
    "name":"Aquaracer Calibre 16 Day-Date",
    "mech":"Calibre 16 Day-Date, механика с автоподзаводом"
  }
]

D
Dmitry Dedukhin, 2015-02-14
@Demetros

If you want to manage the state of the checkboxes in the controller, then each checkbox must be associated with a variable in the controller's scope via ng-model. Setting the variable to true enables the checkbox, setting it to false disables it.
Your array example doesn't have any keys to indicate the state of the checkbox, so I don't quite understand how you are going to know the state of the checkbox later on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question