TWFPSP TWFPSP - 1 month ago 22
AngularJS Question

Can't inject ngRoute dependency in Angular

I can't seem to create a simple angular route in my current application, since it handles me the following error: Uncaught Error: [$injector:modulerr]

I have already injected ngRoute as a dependency in my module, as well as added the angular-route.js script in my html file. My current Angular version is 1.2.25, so it's the angular-route script.

HTML



<html lang="en">
<head>
<meta charset="UTF-8">
<title>ACME App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
<script src="js/controllers.js"></script>
</head>
<body ng-app="init">
<a ng-href="#main">sup</a>
<ng-view></ng-view>
</body>
</html>


controllers.js

var app = angular.module("init", ["ngRoute"]);

app.config('$routeProvider', function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "templates/main.html"
});
});


templates/main.html

<h1>Main Page</h1>

Answer

I think that you missed [ ]

app.config(['$routeProvider', function($routeProvider) {
    $routeProvider
    .when("/", {
        templateUrl : "templates/main.html"
    });
}]);