Runoob Tutorial ArchiveEnglish

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.

FFFCA C5 8A49 47B3 94D6 454 6E1F331

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

'

<

h2

AngularJS 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

<

script

angular.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

</

html

Try it »

Instance parsing:

angular.module('routingDemoApp',['ngRoute'])

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:


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:

$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.

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

</

html

Try it »