AngularJS$location.path()在升级到1.1.15后发生更改
我有一个NavigationController,它为导航列表中的当前选定项设置了selectedItem。它使用$location.path()尝试将其设置为默认值。它处于hashbang模式。以下是一个简化版本:AngularJS$location.path()在升级到1.1.15后发生更改,angularjs,angularjs-routing,Angularjs,Angularjs Routing,我有一个NavigationController,它为导航列表中的当前选定项设置了selectedItem。它使用$location.path()尝试将其设置为默认值。它处于hashbang模式。以下是一个简化版本: App.controller("NavigationController", ['$scope', '$location', function($scope, $location) { $scope.currentSelection = $location.path() ||
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>
问题是,当我导航到/home/index(没有散列爆炸)时,$location.path()返回“/index”,在1.1.15之前,它通常返回null。但是,如果我转到“/home/index#/dashboard”,它会按预期返回“/dashboard”。当有人转到“/to”/dashboard时,我尝试重定向,但导航控制器在重定向之前被调用,因此它会继续获得“/index”
那么,我至少如何知道何时没有包含hashbang呢?$location.hash()似乎总是返回“”。我不想在代码中硬编码“/index”来知道URL上什么时候没有内容。我想您应该使用
$route
服务并挂接到$routeChangeSuccess
事件中
App.controller("NavigationController", function($scope, $location) {
$scope.$on("$routeChangeSuccess", function (scope, next, current) {
$scope.currentSelection = $location.path() || "/dashboard";
});
$scope.select = function( name ) {
$scope.currentSelection = name;
}
});
重定向到:“/
是打字错误吗?它不应该被重定向到:“/”?是的,只是一个简单的打字错误。我正在复制代码并对其进行编辑,以删除与问题无关的部分。
App.controller("NavigationController", function($scope, $location) {
$scope.$on("$routeChangeSuccess", function (scope, next, current) {
$scope.currentSelection = $location.path() || "/dashboard";
});
$scope.select = function( name ) {
$scope.currentSelection = name;
}
});