Search code examples
angularjsangularjs-material

AngularJS multiple checkboxes doubts (Angular Material)


I'm trying to get multiple Angular Material checkboxes with the same ng-model. I have two problems: how to get default checked checkboxes, and how to make at least one of these checkboxes to be required. I tried with ng-checked, but then I can't POST the values through the form.

HTML

      <label for="inputPassword3" class="col-sm-2 control-label">Školski sat *</label>
      <div class="col-sm-10" >
        <span class="col-sm-2" ng-repeat="period in periods">
          <md-checkbox ng-model="form.periods[period]" ng-click="toggle(period, selected)">
            {{ period }}. sat
          </md-checkbox>
        </span>{{selected | json}}
      </div>

App.js

$scope.periods = [1,2,3,4,5,6,7,8,9,0]; /*broj sati*/
      $scope.selected = [2];
      $scope.toggle = function (period, list) {
        var idx = list.indexOf(period);
        if (idx > -1) {
          list.splice(idx, 1);
        }
        else {
          list.push(period);
        }
      };
      $scope.exists = function (period, list) {
        return list.indexOf(period) > -1;
      };

Please, help.


Solution

  • Actually your ngModel is an object, so to get selected value rendered on load, you should do the following:

    $scope.model = {};
    $scope.model.periods = {"2": true};
    

    And to get all selected checkboxes you should iterate over the keys, as below:

    $scope.save = function() {
      // Get all checked boxes
      var checked = Object.keys($scope.model.periods).filter(function(key) {
        return $scope.model.periods[key];
      });
      console.log(checked);
    }
    

    See it working:

    (function() {
      angular
        .module('app', ['ngMaterial'])
        .controller('MainCtrl', MainCtrl);
    
      MainCtrl.$inject = ['$scope'];
    
      function MainCtrl($scope) {
        $scope.model = {};
        $scope.model.periods = {"2": true};
        $scope.periods = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; /*broj sati*/
    
        $scope.save = function() {
          // Get all checked boxes
          var checked = Object.keys($scope.model.periods).filter(function(key) {
            return $scope.model.periods[key];
          });
          console.log(checked);
        }
      }
    })();
    <!DOCTYPE html>
    <html ng-app="app">
    
    <head>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-aria.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-animate.min.js"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.9/angular-material.min.css">
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.9/angular-material.min.js"></script>
    </head>
    
    <body ng-controller="MainCtrl">
      <form name="form">
        <div class="col-md-12">
          <label for="inputPassword3" class="col-sm-2 control-label">Školski sat *</label>
          <div class="col-sm-10">
            <span class="col-sm-2" ng-repeat="period in periods">
              <md-checkbox ng-model="model.periods[period]">
                {{ period }}. sat
              </md-checkbox>
            </span>
          </div>
          <span ng-bind="model.periods | json"></span>
          <hr>
          <button type="button" class="btn btn-success" ng-click="save()">Save data</button>
        </div>
      </form>
    </body>
    
    </html>

    I hope it helps.