AngularJS在routeChangeStartEvent上获取编译的路由URL



我正在尝试在我的angularJS应用程序中创建一个最近访问的菜单,但我似乎找不到获取编译URL 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>

有人对如何干净利落地完成这项工作有任何好主意吗?

使用$location怎么样?

从文档中:

我应该什么时候使用$location?

每当应用程序需要对当前 URL 中的更改做出反应时,或者如果要在浏览器中更改当前 URL。

最新更新