我有一个 NavigationController,它有一个 selectedItem 用于导航列表中的当前选定项目。它使用 $location.path() 尝试将其设置为默认值。它处于 hashbang 模式。这是一个简化版本:
App.controller("NavigationController", ['$scope', '$location', function($scope, $location) {
$scope.currentSelection = $location.path() || "/dashboard";
$scope.select = function( name ) {
$scope.currentSelection = name;
}
}]);
和html:
<body ng-app="App">
<div class="container-fluid absoluteFill">
<div class="row-fluid fill" ng-controller="NavigationController">
<div class="span2 nav-bubble fill">
<ul class="nav nav-list">
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</div>
<div ng-view></div>
</div>
</div>
</body>
和配置:
angular.module("App", ["ngResource"])
.config(function($routeProvider) {
$routeProvider.
when( '/', { redirectTo: '/dashboard' }).
when( '/dashboard', {
controller: 'DashboardController',
templateUrl: '/gpa/app/views/dashboard.html'
}).
otherwise({redirectTo: '/'});
})
问题是,当我导航到 /home/index(没有哈希爆炸)时,$location.path() 返回“/index”,它在 1.1.15 之前曾经返回 null。但是,如果我转到“/home/index#/dashboard”,它会按预期返回“/dashboard”。当有人转到“/”到“/dashboard”时,我尝试重定向,但 NavigationController 在被重定向之前被调用,因此它会继续获取“/索引”。
那么我如何至少知道什么时候不包括 hashbang 呢?$location.hash() 似乎总是返回“”。我不想在我的代码中硬编码“/index”以了解 URL 上什么时候没有。