This example extends the basic example passing parameters in the route in order to use them in the controller
To do so we need to:
$routeParams service in our Controllerapp.js
angular.module('myApp', ['ngRoute'])
.controller('controllerOne', function() {
this.message = 'Hello world from Controller One!';
})
.controller('controllerTwo', function() {
this.message = 'Hello world from Controller Two!';
})
.controller('controllerThree', ['$routeParams', function($routeParams) {
var routeParam = $routeParams.paramName
if ($routeParams.message) {
// If a param called 'message' exists, we show it's value as the message
this.message = $routeParams.message;
} else {
// If it doesn't exist, we show a default message
this.message = 'Hello world from Controller Three!';
}
}])
.config(function($routeProvider) {
$routeProvider
.when('/one', {
templateUrl: 'view-one.html',
controller: 'controllerOne',
controllerAs: 'ctrlOne'
})
.when('/two', {
templateUrl: 'view-two.html',
controller: 'controllerTwo',
controllerAs: 'ctrlTwo'
})
.when('/three', {
templateUrl: 'view-three.html',
controller: 'controllerThree',
controllerAs: 'ctrlThree'
})
.when('/three/:message', { // We will pass a param called 'message' with this route
templateUrl: 'view-three.html',
controller: 'controllerThree',
controllerAs: 'ctrlThree'
})
// redirect to here if no other routes match
.otherwise({
redirectTo: '/one'
});
});
Then, withoud making any changes in our templates, only adding a new link with custom message, we can see the new custom message in our view.
index.html
<div ng-app="myApp">
<nav>
<!-- links to switch routes -->
<a href="/?originalUrl=https%3A%2F%2Friptutorial.com%2F%26quot%3B%23%2Fone%26quot%3B%26gt%3BView%2520One%26lt%3B%2Fa%26gt%3B%2520%2520%2520%2520%26lt%3Ba%2520href%3D%26quot%3B%23%2Ftwo%26quot%3B%26gt%3BView%2520Two%26lt%3B%2Fa%26gt%3B%2520%2520%2520%2520%26lt%3Ba%2520href%3D%26quot%3B%23%2Fthree%26quot%3B%26gt%3BView%2520Three%26lt%3B%2Fa%26gt%3B%2520%2520%2520%2520%26lt%3B!--%2520New%2520link%2520with%2520custom%2520message%2520--%26gt%3B%2520%2520%2520%2520%26lt%3Ba%2520href%3D%26quot%3B%23%2Fthree%2FThis-is-a-message%26quot%3B%26gt%3BView%2520Three%2520with%2520%26quot%3BThis-is-a-message%26quot%3B%2520custom%2520message%26lt%3B%2Fa%26gt%3B%2520%2520%26lt%3B%2Fnav%26gt%3B%2520%2520%26lt%3B!--%2520views%2520will%2520be%2520injected%2520here%2520--%26gt%3B%2520%2520%26lt%3Bdiv%2520ng-view%26gt%3B%26lt%3B%2Fdiv%26gt%3B%2520%2520%26lt%3B!--%2520templates%2520can%2520live%2520in%2520normal%2520html%2520files%2520--%26gt%3B%2520%2520%26lt%3Bscript%2520type%3D%26quot%3Btext%2Fng-template%26quot%3B%2520id%3D%26quot%3Bview-one.html%26quot%3B%26gt%3B%2520%2520%2520%2520%26lt%3Bh1%26gt%3B%7B%7BctrlOne.message%7D%7D%26lt%3B%2Fh1%26gt%3B%2520%2520%26lt%3B%2Fscript%26gt%3B%2520%2520%26lt%3Bscript%2520type%3D%26quot%3Btext%2Fng-template%26quot%3B%2520id%3D%26quot%3Bview-two.html%26quot%3B%26gt%3B%2520%2520%2520%2520%26lt%3Bh1%26gt%3B%7B%7BctrlTwo.message%7D%7D%26lt%3B%2Fh1%26gt%3B%2520%2520%26lt%3B%2Fscript%26gt%3B%2520%2520%26lt%3Bscript%2520type%3D%26quot%3Btext%2Fng-template%26quot%3B%2520id%3D%26quot%3Bview-three.html%26quot%3B%26gt%3B%2520%2520%2520%2520%26lt%3Bh1%26gt%3B%7B%7BctrlThree.message%7D%7D%26lt%3B%2Fh1%26gt%3B%2520%2520%26lt%3B%2Fscript%26gt%3B%26lt%3B%2Fdiv%26gt%3B%253C%2Fcode">