3

I am facing problem with routeProvider, don't know why its not working. The problem is with the config function .Here's the code

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

mainApp.config(['$routeProvider' ,function($routeProvider) {
   $routeProvider.
    when('/', {
        templateUrl: 'index.html',
        controller: 'indexController'
    }).
    when('/first', {
        templateUrl: 'first.html',
        controller: 'firstCtrl'
    }).
    when('/second', {
        templateUrl: 'second.html',
        controller: 'secondCtrl'
    }).
    otherwise({
        redirectTo: 'index.html'
    });
}])

mainApp.controller('indexController', [function() {
    this.outputString = "String from the index controller";
}]);

mainApp.controller('firstCtrl', ['$scope', function($scope) {
    $scope.outputString = "String from the first controller";
}]);

mainApp.controller('secondCtrl',['$scope', function($scope) {
    $scope.outputString = "String from first controller";
}]);

Error Info

3
  • can you add your index page. that will be more helpful Commented Jun 26, 2016 at 13:41
  • what angular version are you using? Commented Jun 26, 2016 at 13:49
  • @SumeetDewangan Did my answer work for you? Let me know if you still have any doubts. Commented Jun 27, 2016 at 11:18

1 Answer 1

1

Your application is missing the angular-route module.

Make sure you add it with:

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-route.min.js"></script>

or, if you are using bower, add "angular-route": "^1.4.0" to bower.json and run bower install.

Last, make sure you are injecting ngRoute into you application

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

Here is a working jsFiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.