Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple toggleClass implemented in AngularJS
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.is-active {
color: red;
}
</style>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="MyCtrl">
<ul>
<li ng-click="toggleClass($event, 'is-active')">Cliccami</li>
<li ng-click="toggleClass($event)">Cliccami</li>
<li ng-click="toggleClass($event, 'is-culo')">Cliccami</li>
<li ng-click="toggleClass($event, 'is-active')">Cliccami</li>
<li ng-click="toggleClass($event, 'is-active')">Cliccami</li>
</ul>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
var myApp = angular.module('myApp', []);
function MyCtrl ($scope) {
$scope.toggleClass = function($event, className) {
className = className || 'is-open';
$($event.target).toggleClass(className);
};
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.