AngularJs routing issue using $routeProvider

Issue

My AngularJs routing always redirects to the home page and always calls HomeController rather than redirecting to /VechileRegistration/Index, VechileRegistration/VechileInward

$routeProvider
  .when('/', {
    controller: 'HomeController',
    templateUrl: '/home/home.view.html',
    controllerAs: 'vm'
  })
  .when('/VechileRegistration/Index', {
    controller: 'HomeController2',
    templateUrl: '/home/home.view2.html',
    controllerAs: 'vm'
  })
  .when('/VechileRegistration/VechileInward', {
    controller: 'VehicleInwardController',
    templateUrl: '/home/VehicleInward.view.html',
    controllerAs: 'vm'
  })
  .otherwise({
    redirectTo: '/login'
  });

How can I redirect to the right place and call the appropriate controller?

Solution

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

    app.config(function ($routeProvider) {
         
        $routeProvider.when('/', {
            templateUrl: '/login.html',
            controller: 'loginController'
        }).when('/student/:username', {
            templateUrl: '/student.html',
            controller: 'studentController'
        }).otherwise({
            redirectTo: "/"
        });

    app.controller("loginController", function ($scope, $location) {
       
        $scope.authenticate = function (username) {
            // write authentication code here.. 

            $location.path('/student/' + username)
        };

    });

    app.controller("studentController", function ($scope, $routeParams) {
        $scope.username = $routeParams.username;
    });

});

Answered By – user1056388

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published