Abolfazl.D.Sh Abolfazl.D.Sh - 16 days ago 6
AngularJS Question

Defining routes for a angular module

I have a mainApp.js file and I put this code on it:

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

**//ROUTES**
mainApp.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/index',
{
templateUrl: '/Views/Admin/index.html'
}
);
}]);


When I run my application without ROUTES section, everything is ok but when I add them to mainApp.js file I face with $injector:modulerr error in my
browser.

I try clear browser cache, check html pages to be sure about including mainApp.js file and replace my codes with these:

var mainApp = angular.module('mainApp', []).config(config);

config.$inject = ['$routeProvider'];
function config($routeProvider) {
$provider
.when('/index',
{
templateUrl: '/Views/Admin/index.html'
}
);
}


But nothing happened and I face with that error again.

can you help me?

thanks

Answer

You need to inject ngRoute as a dependency, also refer the angular-router library,

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

Controller

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

DEMO

Comments