Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Created May 31, 2015 04:15
Show Gist options
  • Save csharpforevermore/cfc2b2b93eb1e9debcbd to your computer and use it in GitHub Desktop.
Save csharpforevermore/cfc2b2b93eb1e9debcbd to your computer and use it in GitHub Desktop.
<label ng-repeat="fruitName in fruits">
<input
type="checkbox"
name="selectedFruits[]"
value="{{fruitName}}"
ng-checked="selection.indexOf(fruitName) > -1"
ng-click="toggleSelection(fruitName)"
> {{fruitName}}
</label>
app.controller('SimpleArrayCtrl', ['$scope', function SimpleArrayCtrl($scope) {
// fruits
$scope.fruits = ['apple', 'orange', 'pear', 'naartjie'];
// selected fruits
$scope.selection = ['apple', 'pear'];
// toggle selection for a given fruit by name
$scope.toggleSelection = function toggleSelection(fruitName) {
var idx = $scope.selection.indexOf(fruitName);
// is currently selected
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
// is newly selected
else {
$scope.selection.push(fruitName);
}
};
}]);
@csharpforevermore
Copy link
Author

Pros: simple data structure and toggling by name is easy to handle

Cons: add/remove is cumbersome as two lists (the input and selection) have to be managed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment