Skip to content

Instantly share code, notes, and snippets.

@PatrickJS
Created October 7, 2013 04:03
Show Gist options
  • Select an option

  • Save PatrickJS/6862373 to your computer and use it in GitHub Desktop.

Select an option

Save PatrickJS/6862373 to your computer and use it in GitHub Desktop.
"Can someone make a gist of the idiomatic way in Angular to navigate between two pages (A and B) where both A and B have a model to fetch?" --Yehuda Katz https://twitter.com/wycats/status/387043646354100224
<!DOCTYPE html>
<html>
<head>
<meta name="fragment" content="!" />
<meta charset="utf-8">
<title>Angular Starter Kit</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css">
<link rel="stylesheet" href="styles.css">
</head>
<body ng-app="App">
<h2>Application Template!</h2>
<nav>
<ul>
<li><a href="#/posts">Posts</a></li>
<li><a href="#/pages">Pages</a></li>
</ul>
</nav>
<ng-view>
<script type="text/ng-template" id="index.html">
<p>Choose Pages or Posts</p>
</script>
<script type="text/ng-template" id="posts.html">
<h3>Posts</h3>
<ul>
<li ng-repeat="post in posts">{{ post.title }} by {{ post.author }}</li>
</ul>
</script>
<script type="text/ng-template" id="pages.html">
<h3>Pages</h3>
<ul>
<li ng-repeat="page in pages">{{ page.title }}</li>
</ul>
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular-route.min.js"></script>
</body>
</html>
angular.module('App', [
'ngRoute'
])
.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'index.html'
})
.when('/posts', {
templateUrl: 'posts.html',
resolve: {
Posts: loadPosts
},
controller: function($scope, Posts) {
$scope.posts = Posts;
}
})
.when('/pages', {
templateUrl: 'pages.html',
resolve: {
Pages: loadPages
},
controller: function($scope, Pages) {
$scope.pages = Pages;
}
});
});
// for simulated Ajax
function loadPosts($q, $timeout) {
var defer = $q.defer();
$timeout(function() {
var posts = [{
id: 1,
title: "Rails is Omakase",
author: "d2h"
}, {
id: 2,
title: "NPM is kind of a big deal",
author: "izs"
}];
defer.resolve(posts);
}, 10); // add more delay
return defer.promise;
}
function loadPages($q, $timeout) {
var defer = $q.defer();
$timeout(function() {
var pages = [{
id: 1,
title: "The Bridge"
}, {
id: 2,
title: "Thank You All"
}];
defer.resolve(pages);
}, 10); // add more delay
return defer.promise;
}
/* Put your CSS here */
html, body {
margin: 20px;
}
nav .active {
font-weight: bold
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment