Routing issue in Angular (happening a "!" in URL)

Issue

I’m using Angular ngRoute.

When I’m trying to route to another page, it is not redirecting in URL. It shows as http://localhost:58317/#!#%2Fstudents

myApp.js

var app = angular.module('MyApp', ['googlechart', 'ngRoute'])
app.controller('HomeCtrl', function ($scope) {
    $scope.msg = "Hello Angular.....";
})

app.config(function ($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(false);
    $locationProvider.hashPrefix("!");
    $routeProvider.when("/home", {
        templateUrl: '/Home/part1',
        controller: 'routeDemoFirstController'
    }).
    when('/contact', {
        templateUrl: '/Home/part2',
        controller: 'routeDemoSecondController'
    }).
     when('/students', {
         templateUrl: '/Home/part2',
         controller: 'routeDemoSecondController'
     })
})

Index.cshtml
This is my .cshtml code Here i write anchor tag

<div class="row">
    <div>
        <a href="/#/home">Home</a>
        <a href="#/contact">Courses</a>
        <a href="#/students">Students</a>
    </div>
    <ng-view></ng-view>
</div>

Solution

You are using the hash-bang prefix:

$locationProvider.hashPrefix("!");

It signicates that your URL should be:

<a href="#!/home">Home</a>

If you want to remove the ! from the URL (you can also remove the #), you can check this answer.

Answered By – Mistalis

Answer Checked By – Mary Flores (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.