Getting $ injector: modulerr after turning on ['ngRoute']

After an error $injector:modulerrafter turning on ngRoute. Using AngularJS 1.2.26


var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider) {
    $routeProvider.when('/', {controller: indexController1, templateURL: 'index1.html'});
    $routeProvider.when('/view/:id', {controller: indexController2, templateURL: 'index2.html'});
    $routeProvider.otherwise({redirectTo: '/'});
});
app.controller('indexController1', function ($scope) { .... }
app.controller('indexController2', function ($scope, $routeParams) { .... }

html template

<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-route.min.js"></script>
<script src="app.js">
</head>
<body>
<div ng-view></div>
</body>
</html>

+4
source share
1 answer

There are some problems in your code:

.config

You should use nested .wheninstead of defining$routeProvider

The name of the controllers between quotation marks

There is no closure );in the controllers

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

app.config(function ($routeProvider) {
    $routeProvider
     .when('/', {
         templateUrl: 'index1.html',
         controller: 'indexController1'
     })
     .when('/view/:id', {
         templateUrl: 'index2.html',
         controller: 'indexController2'
     })
     .otherwise({
         redirectTo: '/'
     });
});

app.controller('indexController1', function ($scope) {

});

app.controller('indexController2', function ($scope, $routeParams) {

});

html

The </script>close tag is missing .

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

Check here for a working ngRoute example

+2
source

All Articles