$routeProvider

The $routeProvider is part of the ngRoute module, which is an official AngularJS module used for basic routing in AngularJS applications. It provides a simple and straightforward way to define routes using a configuration object. With $routeProvider, we can specify the URL pattern and associate it with a template and controller, making it a suitable choice for small to medium-sized projects with relatively simple routing requirements.

Syntax

var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider) {
    $routeProvider
        .when('/routePath', {
            templateUrl: 'template.html',
            controller: 'ControllerName'
        })
        .otherwise({
            redirectTo: '/defaultPath'
        });
});

Example: The below example demonstrates the usage of $routeProvider in AngularJS. Here, when the user clicks on the NavBar links, the application renders the associated route page and displays the content accordingly.

index.html




<!DOCTYPE html>
<html ng-app="myApp">
  
<head>
    <base href="/">
    <script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
      </script>
    <script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular-route.min.js">
      </script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
  
        nav {
            background-color: #333;
        }
  
        ul {
            list-style: none;
            margin: 0;
            padding: 0;
        }
  
        li {
            display: inline;
            margin-right: 20px;
        }
  
        li a {
            text-decoration: none;
            color: #fff;
            font-weight: bold;
            font-size: 18px;
        }
  
        .container {
            text-align: center;
            margin-top: 20px;
        }
  
        h1 {
            color: #4CAF50;
            font-size: 36px;
        }
  
        h3 {
            color: #333;
            font-size: 24px;
        }
    </style>
</head>
  
<body>
    <nav>
        <ul>
            <li><a href="#!/">Home</a></li>
            <li><a href="#!/about">About</a></li>
        </ul>
    </nav>
    <div ng-view class="container">
    </div>
  
    <script>
        var app = angular.module("myApp", ["ngRoute"]);
        app.config(function ($routeProvider) {
            $routeProvider
                .when("/", {
                    templateUrl: "home.html",
                })
                .when("/about", {
                    templateUrl: "about.html",
                })
                .otherwise({
                    redirectTo: "/"
                });
        });
    </script>
</body>
  
</html>


home.html




<div class="container">
    <h1>w3wiki</h1>
    <h3>$routeProvider Example</h3>
    <p>
        w3wiki is a technology education
        platform offering a wide range of 
        tutorials and articles on various
        programming topics.
    </p>
</div>


about.html




<div class="container">
    <h1>About w3wiki</h1>
    <p>
        w3wiki is a computer science 
        portal and is known for its quality 
        content and problem-solving expertise.
        We cover a variety of programming 
        languages, data structures, algorithms,
        and more.
    </p>
    <img src=
"https://media.w3wiki.org/wp-content/uploads/20230816191453/gfglogo.png" 
         alt="w3wiki Logo"
         width="300" 
         height="300" />
</div>


Output

What is the Difference Between $routeProvider and $stateProvider in AngularJS ?

In AngularJS, as we create Single-Page Applications, we need to implement the routing of components to view those images without having full page reloads. This can be achieved with the most popular routing methods, i.e., $routeProvider (ngRoute) and $stateProvider (ui-router).

In this article, we will explore the $routeProvider and $stateProvider in AngularJS and understand their basic implementation with the help of examples, along with knowing the difference between these two methods

Similar Reads

$routeProvider

The $routeProvider is part of the ngRoute module, which is an official AngularJS module used for basic routing in AngularJS applications. It provides a simple and straightforward way to define routes using a configuration object. With $routeProvider, we can specify the URL pattern and associate it with a template and controller, making it a suitable choice for small to medium-sized projects with relatively simple routing requirements....

$stateProvider

...

Difference between $routeProvider and $stateProvider

...