Skip to content

Commit b02190e

Browse files
committed
Add redirect demo.
1 parent dbf4689 commit b02190e

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-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 redirectTo Does Not Break The Back Button In AngularJS](http://bennadel.github.io/JavaScript-Demos/demos/route-redirect-back-button-angularjs/)
1314
* [Exploring $q And Scope $digest Integration In AngularJS](http://bennadel.github.io/JavaScript-Demos/demos/q-scope-digest-integration-angularjs/)
1415
* [Monitoring $http Activity With $http Interceptors In AngularJS](http://bennadel.github.io/JavaScript-Demos/demos/http-interceptors-angularjs/)
1516
* [Monkey-Patching The $q Service Using $provide.decorator() In AngularJS](http://bennadel.github.io/JavaScript-Demos/demos/monkey-patching-q-using-decorate-angularjs/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!doctype html>
2+
<html ng-app="Demo">
3+
<head>
4+
<meta charset="utf-8" />
5+
6+
<title>
7+
$route redirectTo Does Not Break The Back Button In AngularJS
8+
</title>
9+
</head>
10+
<body ng-controller="AppController">
11+
12+
<h1>
13+
$route redirectTo Does Not Break The Back Button In AngularJS
14+
</h1>
15+
16+
<p>
17+
<a href="#/home">Home</a>
18+
&mdash;
19+
<a href="#/people">People</a>
20+
</p>
21+
22+
<p>
23+
<strong>Action</strong>: {{ routeAction }}
24+
</p>
25+
26+
27+
<!-- Load scripts. -->
28+
<script type="text/javascript" src="../../vendor/angularjs/angular-1.3.8.min.js"></script>
29+
<script type="text/javascript" src="../../vendor/angularjs/angular-route-1.3.8.min.js"></script>
30+
<script type="text/javascript">
31+
32+
// Create an application module for our demo.
33+
var app = angular.module( "Demo", [ "ngRoute" ] );
34+
35+
36+
// -------------------------------------------------- //
37+
// -------------------------------------------------- //
38+
39+
40+
// I setup the routes for the application.
41+
app.config(
42+
function( $routeProvider ) {
43+
44+
$routeProvider
45+
.when(
46+
"/home",
47+
{
48+
action: "home"
49+
}
50+
)
51+
52+
// Notice that this route doesn't actually map to an action. Rather,
53+
// it redirects to a sub-route off of the current route.
54+
// --
55+
// NOTE: Under the hood, this uses $location.replace(), which is
56+
// intended to keep the back-button working properly with redirects.
57+
.when(
58+
"/people",
59+
{
60+
redirectTo: "/people/friends"
61+
}
62+
)
63+
64+
.when(
65+
"/people/friends",
66+
{
67+
action: "people.friends"
68+
}
69+
)
70+
.when(
71+
"/people/enemies",
72+
{
73+
action: "people.enemies"
74+
}
75+
)
76+
.otherwise({
77+
redirectTo: "/home"
78+
})
79+
;
80+
81+
}
82+
);
83+
84+
85+
// -------------------------------------------------- //
86+
// -------------------------------------------------- //
87+
88+
89+
// I control the root of the application.
90+
app.controller(
91+
"AppController",
92+
function( $scope, $route, $routeParams ) {
93+
94+
$scope.routeAction = null;
95+
96+
// I listen for route-change events.
97+
$scope.$on(
98+
"$routeChangeSuccess",
99+
function handleRouteChangeEvent( event ) {
100+
101+
var current = $route.current;
102+
103+
console.log( "Route change event:", current.originalPath );
104+
105+
// If the current route doesn't contain an action, then it will,
106+
// in all likelihood, be redirected to another route that does
107+
// contain a valid action (configured in $routeProvider).
108+
if ( ! current.action ) {
109+
110+
console.warn(
111+
"Route does not contain an action.",
112+
"Redirecting to another route."
113+
);
114+
115+
}
116+
117+
// Store our current action for output.
118+
$scope.routeAction = current.action;
119+
120+
}
121+
);
122+
123+
}
124+
);
125+
126+
</script>
127+
128+
</body>
129+
</html>

0 commit comments

Comments
 (0)