Skip to content

Commit fed5137

Browse files
committed
Add route resolution function demo.
1 parent be475eb commit fed5137

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ with.
1010

1111
## My JavaScript Demos - I Love JavaScript!
1212

13+
* [Route Resolution Using Factory Functions vs. Services In AngularJS](http://bennadel.github.io/JavaScript-Demos/demos/route-resolution-factory-angularjs/)
1314
* [Exploring $route Resolution In AngularJS](http://bennadel.github.io/JavaScript-Demos/demos/route-resolve-angularjs/)
1415
* [$route redirectTo Will Pass-Through Current Route Params In AngularJS](http://bennadel.github.io/JavaScript-Demos/demos/route-redirect-params-angularjs/)
1516
* [$route redirectTo Does Not Break The Back Button In AngularJS](http://bennadel.github.io/JavaScript-Demos/demos/route-redirect-back-button-angularjs/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<!doctype html>
2+
<html ng-app="Demo">
3+
<head>
4+
<meta charset="utf-8" />
5+
6+
<title>
7+
Route Resolution Using Factory Functions vs. Services In AngularJS
8+
</title>
9+
</head>
10+
<body>
11+
12+
<h1>
13+
Route Resolution Using Factory Functions vs. Services In AngularJS
14+
</h1>
15+
16+
<p>
17+
<a href="#/a">Section A</a>
18+
&mdash;
19+
<a href="#/b">Section B</a>
20+
</p>
21+
22+
23+
<!-- Load scripts. -->
24+
<script type="text/javascript" src="../../vendor/angularjs/angular-1.3.8.min.js"></script>
25+
<script type="text/javascript" src="../../vendor/angularjs/angular-route-1.3.8.min.js"></script>
26+
<script type="text/javascript">
27+
28+
// Create an application module for our demo.
29+
var app = angular.module( "Demo", [ "ngRoute" ] );
30+
31+
32+
// -------------------------------------------------- //
33+
// -------------------------------------------------- //
34+
35+
36+
// I setup the routes for the application.
37+
app.config(
38+
function( $routeProvider ) {
39+
40+
// Define routes for the application.
41+
$routeProvider
42+
.when(
43+
"/a",
44+
{
45+
resolve: {
46+
// In this case, we're going to define the route
47+
// resolution as a factory function. This function will
48+
// be invoked every time the route is accessed and should
49+
// return a value or a promise.
50+
// --
51+
// NOTE: Accessed using $injector.invoke().
52+
aResolutionFactory: function() {
53+
54+
console.log( "A-resolver" );
55+
56+
return( "a-value-or-promise" );
57+
58+
}
59+
}
60+
}
61+
)
62+
.when(
63+
"/b",
64+
{
65+
resolve: {
66+
// In this case, we're going to define the route
67+
// resolution as a service. The String here represents
68+
// the name of a service within the Dependency-Injection
69+
// container. This service will be instantiated the first
70+
// time that this route is accessed. For every subsequent
71+
// route access, the already-instantiated service is used.
72+
// --
73+
// NOTE: Accessed using $injector.get().
74+
bResolutionService: "bResolutionService"
75+
}
76+
}
77+
)
78+
79+
// And, if nothing else matches, just redirect to section A.
80+
.otherwise({
81+
redirectTo: "/a"
82+
})
83+
;
84+
85+
}
86+
);
87+
88+
89+
// -------------------------------------------------- //
90+
// -------------------------------------------------- //
91+
92+
93+
// I provide a service that loads a route resource and returns the resolved
94+
// value (or a promise of the resolved value).
95+
app.factory(
96+
"bResolutionService",
97+
function( $q ) {
98+
99+
console.log( "B-resolver" );
100+
101+
return( $q.when( "b-value-or-promise" ) );
102+
103+
}
104+
);
105+
106+
107+
// -------------------------------------------------- //
108+
// -------------------------------------------------- //
109+
110+
111+
// Since we don't have any controllers in this demo, we have to inject the
112+
// $route service at least ONCE in order to get the routing module to fully load.
113+
// Without this step, none of the other routing will work.
114+
app.run(
115+
function loadRoute( $route ) {
116+
117+
// Just forcing $route service to be instantiated.
118+
119+
}
120+
)
121+
122+
</script>
123+
124+
</body>
125+
</html>

0 commit comments

Comments
 (0)