AngularJS Routing
AngularJS Routing
In this chapter, we will introduce AngularJS Routing to you.
AngularJS Routing allows us to access different content through different URLs.
AngularJS enables single page web applications (SPA) with multiple views.
Usually, our URL format ishttp://runoob.com/first/page, but in a single page web application, AngularJS uses#! + markerImplementation, for example:
http://runoob.com/#!/first
http://runoob.com/#!/second
http://runoob.com/#!/third
Note that versions before Angular1.6 use# + markersto implement routing.
When we click any of the links above, the address requested from the server is the same (http://runoob.com/). Because the content after the #! is ignored by the browser when making a server request, we need to implement the functionality of the content after #! on the client side. AngularJS routing is achieved through#! + markersto help us distinguish different logical pages and bind different pages to corresponding controllers.

In the diagram above, we can see that two URLs have been created: /ShowOrders and /AddNewOrder. Each URL has a corresponding view and controller.
Next, let's look at a simple example:
AngularJS instance
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>AngularJS Routing Instance - Cailiao Tutorial
</
title
<
script
src
=
"
https://cdn.bootcss.com/angular.js/1.7.0/angular.min.js
"
</
script
<
script
src
=
"
https://cdn.bootcss.com/angular.js/1.7.0/angular-route.min.js
"
</
script
</
head
<
body
ng-app
='
routingDemoApp
'
<
h2AngularJS routing application
</
h2
<
ul
<
li
<
a
href
=
"
#!/
"
Home
</
a
</
li
<
li
<
a
href
=
"
#!/computers
"
Computer
</
a
</
li
<
li
<
a
href
=
"
#!/printers
"
Printer
</
a
</
li
<
li
<
a
href
=
"
#!/blabla
"
Other
</
a
</
li
</
ul
<
div
ng-view
</
div
<
scriptangular.module('routingDemoApp',['ngRoute']) .config(['$routeProvider', function($routeProvider){ $routeProvider .when('/',{template:'This is the homepage'}) .when('/computers',{template:'This is the computer category page'}) .when('/printers',{template:'This is the printer page'}) .otherwise({redirectTo:'/'}); }]);
</
script
</
body
</
htmlTry it »
Instance parsing:
-
1. Loaded the js file implementing routing: angular-route.js.
-
2. Includes the ngRoute module as a dependency module for the main application.
angular.module('routingDemoApp',['ngRoute'])
-
3. Uses the ngView directive.
<div ng-view></div>
The HTML content inside this div will change according to the route changes.
-
4. Configures $routeProvider, which is used by AngularJS $routeProvider to define routing rules.
module.config(['$routeProvider', function($routeProvider){ $routeProvider .when('/',{template:'This is the homepage'}) .when('/computers',{template:'This is the computer category page'}) .when('/printers',{template:'This is the printer page'}) .otherwise({redirectTo:'/'}); }]);
The config function of the AngularJS module is used to configure routing rules. By using the configAPI, we request to inject $routeProvider into our configuration function and use the $routeProvider.whenAPI to define our routing rules.
$routeProvider provides us with the when(path,object) & otherwise(object) functions to define all routes sequentially, the function contains two parameters:
- The first parameter is a URL or a URL regular expression.
- The second parameter is a route configuration object.
Route setting object
AngularJS routing can also be implemented through different templates.
The first parameter of the $routeProvider.when function is a URL or a URL regular expression, and the second parameter is a route configuration object.
The syntax rules for the route configuration object are as follows:
Original prompt (Chinese, left untranslated)
$routeProvider.when(url, {
template: string,
templateUrl: string,
controller: string, function 或 array,
controllerAs: string,
redirectTo: string, function,
resolve: object<key, function>
});
Parameter description:
-
template:
If we only need to insert simple HTML content in ng-view, use this parameter:
Original prompt (Chinese, left untranslated)
.when('/computers',{template:'这是电脑分类页面'}) -
templateUrl:
If we only need to insert an HTML template file in ng-view, use this parameter:
$routeProvider.when('/computers', { templateUrl: 'views/computers.html', });
The above code will fetch the content of views/computers.html from the server and insert it into ng-view.
-
controller:
function, string, or array type, the controller function executed on the current template to generate a new scope.
-
controllerAs:
string type, specifies an alias for the controller.
-
redirectTo:
The address to redirect to.
-
resolve:
Specifies other modules that the current controller depends on.
instance
AngularJS instance
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>AngularJS Routing Instance - Cailiao Tutorial
</
title
<
script
src
=
"
https://cdn.bootcss.com/angular.js/1.7.0/angular.min.js
"
</
script
<
script
src
=
"
https://cdn.bootcss.com/angular.js/1.7.0/angular-route.min.js
"
</
script
<
script
type
=
"
text/javascript
"angular.module('ngRouteExample', ['ngRoute']) .controller('HomeController', function ($scope, $route) { $scope.$route = $route;}) .controller('AboutController', function ($scope, $route) { $scope.$route = $route;}) .config(function ($routeProvider) { $routeProvider. when('/home', { templateUrl: 'embedded.home.html', controller: 'HomeController' }). when('/about', { templateUrl: 'embedded.about.html', controller: 'AboutController' }). otherwise({ redirectTo: '/home' }); });
</
script
</
head
<
body
ng-app
=
"
ngRouteExample
"
class
=
"
ng-scope
"
<
script
type
=
"
text/ng-template
"
id
=
"
embedded.home.html
"
<
h1
Home
</
h1
</
script
<
script
type
=
"
text/ng-template
"
id
=
"
embedded.about.html
"
<
h1
About
</
h1
</
script
<
div
<
div
id
=
"
navigation
"
<
a
href
=
"
#!/home
"
Home
</
a
<
a
href
=
"
#!/about
"
About
</
a
</
div
<
div
ng-view
=
"
"
</
div
</
div
</
body
</
htmlTry it »