Created
March 3, 2016 11:57
-
-
Save japharr/3b9cba3acb0545742cb5 to your computer and use it in GitHub Desktop.
In AngularJS How do you check if any Checkbox is Checked or Unchecked?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> | |
</head> | |
<body> | |
<div ng-app="myApp" ng-controller="myController" | |
ng-init="list=[ | |
{ name:'Computer Architecture' }, | |
{ name:'Advanced Composite Materials'}, | |
{ name:'Stategies Unplugged' }, | |
{ name:'Teaching Science' }, | |
{ name:'Challenging Times' }]" | |
> | |
<div ng-repeat="books in list"> | |
<input type="checkbox" ng-model="books.selected" | |
ng-true-value="'{{books.name}}'" ng-false-value="''" | |
id="'{{books.name}}'" /> | |
{{ books.name }} | |
</div> | |
<p><button ng-click="submit(list)">Submit</button></p> | |
<p>{{the_list}}</p> | |
</div> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myApp = angular.module('myApp', []); | |
myApp.controller('myController', ['$scope', function ($form) { | |
$form.submit = function (list) { | |
var books = []; | |
angular.forEach(list, function (value, key) { | |
if (list[key].selected == list[key].name) { | |
books.push(list[key].selected); | |
} | |
}); | |
// SHOW THE SELECTED ITEMS IN THE EXPRESSION. | |
if (books.length > 0) | |
$form.the_list = books.toString(); | |
else | |
$form.the_list = 'Please choose an option'; | |
} | |
}] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment