Tutorials  /  JavaScript

Effective Routing in AngularJS: A Comprehensive Example with ngRoute and $routeProvider

Ccentron Redaktion · October 2024 ·3 min read ·JavaScript, Tutorial

Explore the power of AngularJS routing with ngRoute and $routeProvider in our blog post. Learn how to create complex single-page applications using this powerful feature, providing seamless navigation and interaction. Dive into practical examples and a step-by-step guide to implementing it into your own projects.

Introduction

AngularJS provides mechanisms for routing in web applications with the ngRoute module and the $routeProvider. Defined routing allows different contents to be loaded and displayed based on the URL without reloading the page.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Using ngRoute

The ngRoute module of AngularJS provides routing services and directives. To utilize the routing feature, the angular-route.js script must be downloaded, which contains the ngRoute module. Alternatively, the module can also be included via CDN.

Code
<   script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28//angular-route.min.js"> <   /script>

Configuring $routeProvider

The $routeProvider is used to configure routes. The config() function configures the $routeProvider by defining routes that respond to specific URLs. The $routeProvider offers a simple API that accepts either the when() or otherwise() method.

Code
var mainApp = angular.module("mainApp", ['ngRoute']);

mainApp.config(function($routeProvider) {
  $routeProvider
    .when('/home', {
      templateUrl: 'home.html',
      controller: 'StudentController'
    })
    .when('/viewStudents', {
      templateUrl: 'viewStudents.html',
      controller: 'StudentController'
    })
    .otherwise({
      redirectTo: '/home'
    });
});

Example Application

To demonstrate routing in AngularJS, we'll create a simple application with two views: a home page and a page to display student information.

Code
mainApp.controller('StudentController', function($scope) {
  $scope.students = [
    {name: 'Mark Waugh', city:'New York'},
    {name: 'Steve Jonathan', city:'London'},
    {name: 'John Marcus', city:'Paris'}
  ];

  $scope.message = "Click on the hyperlink to view the students list.";
});

Conclusion

By using AngularJS routing, complex single-page applications can be created, offering user-friendly navigation and interaction without page reloads. AngularJS routing enables you to build effective single-page applications and enhance user experience. It's a powerful tool for structuring and organizing complex applications. If you want to learn more about AngularJS, explore our online demo and delve deeper into the world of AngularJS routing!

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren