如果没有任何代码示例或笨蛋,很难回答您的问题。我实现了一个 plunker 代码(http://plnkr.co/edit/oflB21?p=preview)尝试重现此问题,但如您所见,我无法重现此问题。即您可以轻松地在页面的两个不同部分之间来回导航,例如在 #/Country/Italy#Section-4 和 #/Country/Italy#Section-1 之间,无需任何页面加载或重新路由。请在下面的 plunker 中查看我的工作示例。这很可能是由于缺少哈希符号或正斜杠或类似的细节而发生在您身上。
主页的 HTML 片段:
<ul>
<li><a href="#/Country">Go to /Country</a></li>
<li><a href="#/Country/US">Go to /Country/US</a></li>
<li><a href="#/Country/Italy#Section-4">Go to /Country/Italy#Section-4</a></li>
<li><a href="#/Country/Canada#Section-8">Go to /Country/Canada#Section-8</a></li>
</ul>
国家/地区页面的 HTML 片段:
<div id="Section-1" class="section pink">
Section 1
<div ng-if="country">
<a ng-href="#/Country/{{country}}#Section-8">Go to /Country/{{country}}#Section-8</a>
</div>
<div ng-if="!country">
<a ng-href="#/Country#Section-8">Go to /Country#Section-8</a>
</div>
</div>
所有 JavaScript 代码:
var app = angular.module("app", ["ngRoute"]);
app.config(["$routeProvider", "$locationProvider",
function ($routeProvider, $locationProvider) {
$routeProvider
.when("/", {
templateUrl: "./home-page.html",
caseInsensitiveMatch: true,
})
.when("/Home", {
templateUrl: "./home-page.html",
caseInsensitiveMatch: true,
})
.when("/Country", {
templateUrl: "./country-page.html",
caseInsensitiveMatch: true,
})
.when("/Country/:country", {
templateUrl: "./country-page.html",
caseInsensitiveMatch: true,
})
}]);
countryController.$inject = ["$scope", "$routeParams", "$location", "$anchorScroll"];
function countryController($scope, $routeParams, $location, $anchorScroll) {
$scope.country = $routeParams.country;
if (!!$location.$$hash) {
$location.hash($location.$$hash);
$anchorScroll();
}
}