Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple AngularJS routing not working

Home and About page have simple heading content which is not showing when running the page. Instead it is showing:

Error: $injector:modulerr
Module Error".

How do I fix this?

var app = angular.module('myApp', ['ngRoute']);

app.config(function ($routeProvider) {
    $routeProvider.
        .when('/', {
        	templateUrl : 'partial/home.html',
        })
        .when('/about', {
        	templateUrl: 'partial/about.html',
        })
        otherwise({ redirectTo: '/'})
});
<div class="container">
    <div>
      <nav>
          <a href="#/">Home</a>
          <a href="#/about">About</a>
        </nav>
    </div>
</div>
like image 419
Learner Avatar asked Feb 04 '26 20:02

Learner


1 Answers

Although I haven't run your code, I can see a syntax error.

EDIT: Actually you have two syntax errors, rather than missing out a '.', you've added two after $routeProvider.

app.config(function ($routeProvider) {
$routeProvider. //Two '.'s 
    .when('/', {
        templateUrl : 'partial/home.html',

    })
    .when('/about', {
        templateUrl: 'partial/about.html',

    })
    //No '.' here
    otherwise({ redirectTo: '/'})
});

So replace the above with the '.', so the function chaining doesn't break.

app.config(function ($routeProvider) {
$routeProvider
    .when('/', {
        templateUrl : 'partial/home.html',

    })
    .when('/about', {
        templateUrl: 'partial/about.html',

    })

    .otherwise({ redirectTo: '/'})
});
like image 56
Lee Brindley Avatar answered Feb 06 '26 09:02

Lee Brindley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!