The initial setup:
<body ng-app="myApp">
<ng-view></ng-view>
</body>
angular.module('myApp', ['ngRoute'])
.config(function($routeProvider){
$routeProvider.when("/",
{
templateUrl: "app.html",
controller: "AppCtrl",
controllerAs: "app"
}
);
})
.controller('AppCtrl', function() {
var self = this;
self.message = "The app routing is working!";
});
<h1>{{ app.message }}</h1>
$routeProvider has a simple API, accepting either the when() method, which matches a pattern, or otherwise(). It also allows for method chaining:
.config(function($routeProvider){
$routeProvider.when("/",
{
templateUrl: "app.html",
controller: "AppCtrl",
controllerAs: "app"
}
)
.when('/cookies',
{
template: "NOM NOM NOM NOM"
}
)
.otherwise({
template: "This route isn't set!"
});
})
Otherwise isn’t passed a route because it functions as a catch-all for bad routes.