Javascript AngularJS在routeChangeStartEvent上获取已编译的路由URL
我正在尝试在angularJS应用程序中创建一个最近访问过的菜单,但我似乎找不到一个好方法来获取编译后的URLJavascript AngularJS在routeChangeStartEvent上获取已编译的路由URL,javascript,angularjs,Javascript,Angularjs,我正在尝试在angularJS应用程序中创建一个最近访问过的菜单,但我似乎找不到一个好方法来获取编译后的URLrouteChangeStart事件,它只是一个模板 我的代码如下所示: $rootScope.$on("$routeChangeStart", function (event, next, current) { if ($route.current && $route.current.$$route && $rou
routeChangeStart
事件,它只是一个模板
我的代码如下所示:
$rootScope.$on("$routeChangeStart", function (event, next, current) {
if ($route.current && $route.current.$$route && $route.current.$$route.controller){
// only show 5
if($scope.previousLocations.length == 5){
$scope.previousLocations.pop();
}
var title = $route.current.$$route.title,
hasTitleAlready = false;
// only add them once
angular.forEach($scope.previousLocations, function(obj){
if(obj.title == title){
hasTitleAlready = true;
return false;
}
});
if(hasTitleAlready){
$scope.previousLocations.push({
title: title,
link: 'the compiled link here'
});
}
}
});
<ul class="recent-view">
<li ng-repeat="loc in previousLocations"><a href="{{loc.link}}">{{loc.title}}</a></li>
</ul>
我的模板如下所示:
$rootScope.$on("$routeChangeStart", function (event, next, current) {
if ($route.current && $route.current.$$route && $route.current.$$route.controller){
// only show 5
if($scope.previousLocations.length == 5){
$scope.previousLocations.pop();
}
var title = $route.current.$$route.title,
hasTitleAlready = false;
// only add them once
angular.forEach($scope.previousLocations, function(obj){
if(obj.title == title){
hasTitleAlready = true;
return false;
}
});
if(hasTitleAlready){
$scope.previousLocations.push({
title: title,
link: 'the compiled link here'
});
}
}
});
<ul class="recent-view">
<li ng-repeat="loc in previousLocations"><a href="{{loc.link}}">{{loc.title}}</a></li>
</ul>
有人对如何干净利落地完成这项任务有什么好主意吗?如何使用
从文件中:
我什么时候应该使用$location?
当您的应用程序需要对当前URL的更改做出反应时,或者如果您希望在浏览器中更改当前URL,则可以随时进行响应
$location是“新”路由,而不是我试图保留的路由。是否有更好的事件可以让我在更改前显示位置?@amcdnl否,您只需缓存该值并在需要时使用它。