Skip to content

Instantly share code, notes, and snippets.

@HAKASHUN
Created October 17, 2013 05:22
Show Gist options
  • Save HAKASHUN/7019497 to your computer and use it in GitHub Desktop.
Save HAKASHUN/7019497 to your computer and use it in GitHub Desktop.
AngularJSでng-classを使ってみた。
<html ng-app>
<head>
<meta charset="UTF-8">
<title>restaurantTable</title>
<style>
.selected {
background: lightgreen;
}
</style>
</head>
<body>
<table ng-controller='RestaurantTableController'>
<!--
ng-classの指定
クリックした<tr>のindexとselextedRowが一致したら selectedクラスをつける
-->
<tr ng-repeat='restaurant in directory'
ng-click='selectRestaulant($index)'
ng-class='{selected: $index==selectedRow}'>
<td>{{restaurant.name}}</td>
<td>{{restaurant.cuisine}}</td>
</tr>
</table>
<script src="../bower_components/angular/angular.js"></script>
<script>
function RestaurantTableController($scope) {
$scope.directory = [
{name: 'The Handsome Heifer', cuisine:'BBQ'},
{name: 'Green\'s Green Greend', cuisine:'Salads'},
{name: 'House of Fine Fish', cuisine:'Seafood'}
];
$scope.selectRestaulant = function(row) {
$scope.selectedRow = row;
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment