Skip to content

Instantly share code, notes, and snippets.

@anonymoussc
Created October 25, 2015 16:04
Show Gist options
  • Save anonymoussc/954ebf4ea3945216e06d to your computer and use it in GitHub Desktop.
Save anonymoussc/954ebf4ea3945216e06d to your computer and use it in GitHub Desktop.
Using Directives to Handle Events
<!DOCTYPE html>
<html ng-app="exampleApp">
<head>
<title>Directives</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script>
angular.module("exampleApp", [])
.controller("defaultCtrl", function($scope) {
$scope.todos = [
{action: "Get groceries", complete: false},
{action: "Call plumber", complete: false},
{action: "Buy running shoes", complete: true},
{action: "Buy flowers", complete: false},
{action: "Call family", complete: false}];
$scope.buttonNames = ["Red", "Green", "Blue"];
$scope.data = {
rowColor : "Blue",
columnColor: "Green"
};
$scope.handleEvent = function(e) {
console.log("Event type: " + e.type);
$scope.data.columnColor = e.type == "mouseover" ? "Green" : "Blue";
}
});
</script>
<style>
.Red {
background-color : lightcoral;
}
.Green {
background-color : lightgreen;
}
.Blue {
background-color : lightblue;
}
</style>
</head>
<body>
<div id="todoPanel" class="panel" ng-controller="defaultCtrl">
<h3 class="panel-header">To Do List</h3>
<div class="well">
<span ng-repeat="button in buttonNames">
<button class="btn btn-info" ng-click="data.rowColor = button">
{{button}}
</button>
</span>
</div>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Action</th>
<th>Done</th>
</tr>
</thead>
<tr ng-repeat="item in todos" ng-class="data.rowColor"
ng-mouseenter="handleEvent($event)"
ng-mouseleave="handleEvent($event)">
<td>{{$index + 1}}</td>
<td>{{item.action}}</td>
<td ng-class="data.columnColor">{{item.complete}}</td>
</tr>
</table>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment