Skip to content

Instantly share code, notes, and snippets.

@jeshan
Last active August 29, 2015 14:15
Show Gist options
  • Save jeshan/772353251d63747639f6 to your computer and use it in GitHub Desktop.
Save jeshan/772353251d63747639f6 to your computer and use it in GitHub Desktop.
angular-intro-10; routes
<!DOCTYPE html>
<html ng-app='angularApp'>
<head>
<script src="//code.jquery.com/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<meta name="description" content="angular-intro-10; routes">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.min.js"></script>
<script src="script.js">
</script>
<meta charset="utf-8">
<script type='text/ng-template' id='/views/main.html'>
<p class='h3'>Main View</p>
<table class='table table-striped'>
<tr>
<th>id</th>
<th>Name</th>
<th></th>
</tr>
<tbody>
<tr ng-repeat='customer in customers'>
<td ng-bind='customer.id'></td>
<td ng-bind='customer.name'></td>
<td><a ng-href='#/detail/{{customer.id}}'>Detail</a></td>
</tr>
</tbody>
</table>
</script>
<script type='text/ng-template' id='/views/detail.html'>
<a href="#/">Back to Main</a><br/>
<p class='h3'>Detail view</p>
<table class='table table-striped'>
<tr>
<th>id</th>
<th>Name</th>
<th>Gender</th>
<th>Country</th>
</tr>
<tbody>
<tr>
<td ng-bind='customer.id'></td>
<td ng-bind='customer.name'></td>
<td ng-bind='customer.gender'></td>
<td ng-bind='customer.country'></td>
</tr>
</tbody>
</table>
</script>
</head>
<body ng-controller='MainController'>
<div ng-view></div>
</body>
</html>
var module = angular.module('angularApp', ['ngRoute']);
module.config(function($routeProvider) {
$routeProvider
.when('/', {
controller: 'MainController',
templateUrl: '/views/main.html'
})
.when('/detail/:id', {
controller: 'DetailController',
templateUrl: '/views/detail.html'
})
;
});
module.controller('MainController', function($scope, CustomerService) {
$scope.customers = CustomerService.getCustomers();
});
module.controller('DetailController', function($scope, CustomerService, $routeParams) {
$scope.customer = CustomerService.getCustomerById($routeParams.id);
});
module.service('CustomerService', function() {
var customers = [{
id: 1,
name: 'John Doe',
gender: 'Male',
country: 'Mauritius'
}, {
id: 2,
name: 'Jane Doe',
gender: 'Female',
country: 'United Kingdom'
}];
this.getCustomers = function() {
return customers;
};
this.getCustomerById = function(id) {
var result = {};
angular.forEach(this.getCustomers(), function(item) {
if (item.id == id) {
result = item;
}
});
return result;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment