Wednesday, 25 January 2017

What is Routing in AngularJS?

AngularJS Routing helps you to divide your app into multiple views and bind different views to Controllers. The magic of Routing is taken care by an AngularJS service $routeProvider. $routeProvider service provides method when() and otherwise() to define the routes for your app. Routing has dependency on ngRoute module.

Defining Route for your application

 <script type="text/javascript">
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/products', { //route
templateUrl: 'views/products.html',
controller: 'productController'
}).
when('/product/:productId', { //route with parameter
templateUrl: 'views/product.html',
controller: 'productController'
}).
otherwise({ //default route
redirectTo: '/index'
});
}]);
</script>

No comments:

Post a Comment