Angular 中的路由问题(在 URL 中出现“!”)

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

我正在使用 Angular ngRoute

当我试图路由到另一个页面时,它没有在 URL 中重定向。它显示为 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 这是我的 .cshtml 代码我在这里写锚标签

<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>

您正在使用 hash-bang 前缀:

$locationProvider.hashPrefix("!");

表示您的URL应该是:

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

如果您想从 URL 中删除 !您也可以删除 #),您可以检查 .

删除

$locationProvider.hashPrefix("!");

更改 html

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