Skip to content

Instantly share code, notes, and snippets.

@superchris
Created May 4, 2015 21:51
Show Gist options
  • Select an option

  • Save superchris/fcd7368c61dd1d94999e to your computer and use it in GitHub Desktop.

Select an option

Save superchris/fcd7368c61dd1d94999e to your computer and use it in GitHub Desktop.
JS Bin Fruit router example // source http://jsbin.com/mumana
<!DOCTYPE html>
<html ng-app="fruit">
<head>
<meta name="description" content="Fruit router example" />
<script src="http://code.jquery.com/jquery.min.js"></script>
<link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap-responsive.css" rel="stylesheet" type="text/css" />
<script src="http://getbootstrap.com/2.3.2/assets/js/bootstrap.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="http://code.angularjs.org/1.3.15/angular-route.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body ng-controller="FruitCtrl as ctrl">
<ul>
<li ng-repeat="fruit in ctrl.fruits"><a href="#/fruits/{{fruit.name}}">{{fruit.name}}</a></li>
</ul>
<ng-view />
<script id="jsbin-javascript">
fruit = angular.module("fruit", ["ngRoute"]);
fruit.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/fruits/:fruitName', {templateUrl: 'templates/fruit.html', controller: "ShowFruitCtrl as showCtrl"});
}]);
fruit.factory("Fruit", function() {
return {
fruits: [
{name: "Bananas", description: "Yellow and peely"},
{name: "Canteloupe", description: "Tell if its ripe by smelling them"},
{name: "Cherries", description: "Dont try to make jam out of sweet ones"},
{name: "Strawberries", description: "Picking them is murder on your back"},
{name: "Tomatoes", description: "People used to think they were poisonous" }
],
getFruit: function(fruitName) {
return _.findWhere(this.fruits, {name: fruitName});
}
};
});
fruit.controller("FruitCtrl", function(Fruit) {
this.fruits = Fruit.fruits;
});
fruit.controller("ShowFruitCtrl", function(Fruit, $routeParams) {
this.fruit = Fruit.getFruit($routeParams.fruitName);
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">fruit = angular.module("fruit", ["ngRoute"]);
fruit.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/fruits/:fruitName', {templateUrl: 'templates/fruit.html', controller: "ShowFruitCtrl as showCtrl"});
}]);
fruit.factory("Fruit", function() {
return {
fruits: [
{name: "Bananas", description: "Yellow and peely"},
{name: "Canteloupe", description: "Tell if its ripe by smelling them"},
{name: "Cherries", description: "Dont try to make jam out of sweet ones"},
{name: "Strawberries", description: "Picking them is murder on your back"},
{name: "Tomatoes", description: "People used to think they were poisonous" }
],
getFruit: function(fruitName) {
return _.findWhere(this.fruits, {name: fruitName});
}
};
});
fruit.controller("FruitCtrl", function(Fruit) {
this.fruits = Fruit.fruits;
});
fruit.controller("ShowFruitCtrl", function(Fruit, $routeParams) {
this.fruit = Fruit.getFruit($routeParams.fruitName);
});
</script></body>
<script id="templates/fruit.html" type="text/ng-template">
<dl>
<dt>{{showCtrl.fruit.name}}</dt>
<dd>{{showCtrl.fruit.description}}</dd>
</dl>
</script>
</html>
fruit = angular.module("fruit", ["ngRoute"]);
fruit.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/fruits/:fruitName', {templateUrl: 'templates/fruit.html', controller: "ShowFruitCtrl as showCtrl"});
}]);
fruit.factory("Fruit", function() {
return {
fruits: [
{name: "Bananas", description: "Yellow and peely"},
{name: "Canteloupe", description: "Tell if its ripe by smelling them"},
{name: "Cherries", description: "Dont try to make jam out of sweet ones"},
{name: "Strawberries", description: "Picking them is murder on your back"},
{name: "Tomatoes", description: "People used to think they were poisonous" }
],
getFruit: function(fruitName) {
return _.findWhere(this.fruits, {name: fruitName});
}
};
});
fruit.controller("FruitCtrl", function(Fruit) {
this.fruits = Fruit.fruits;
});
fruit.controller("ShowFruitCtrl", function(Fruit, $routeParams) {
this.fruit = Fruit.getFruit($routeParams.fruitName);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment