0

I want to change columns: [...] depend on checkbox.

If I check the checkbox then it should give me columns: ['model.name' ,'model.department']

and when I uncheck it should change it to columns: ['model.name'] - removed model.department

angular.module('plunker', [])

.controller('MainCtrl', function($scope) {
  $scope.options = {
    columns: ['model.name', 'model.department'],
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script data-require="angular.js@1.*" data-semver="1.4.3" src="https://code.angularjs.org/1.4.3/angular.js"></script>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script src="app.js"></script>
</head>



<body ng-controller="MainCtrl">
  <li>
    <label>
      <input type="checkbox" ng-model=options.columns>Department</label>
  </li>
</body>

</html>

1 Answer 1

3

Add a function to the scope in the controller to add or remove the item depending on if the checkbox is checked or not:

$scope.addRemoveItem = function (checked, value) {
  if (checked) {
    $scope.options.columns.push(value);
  }
  else {
    var index = $scope.options.columns.indexOf(value);
    $scope.options.columns.splice(index);
  }
};

Then use ng-change to call this function when the checkbox is changed:

<label><input type="checkbox" ng-model="departmentChecked" ng-change="addRemoveItem(departmentChecked, 'model.department')">Department</label>

Plunkr

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.