Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS在routeChangeStartEvent上获取已编译的路由URL_Javascript_Angularjs - Fatal编程技术网

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

Javascript AngularJS在routeChangeStartEvent上获取已编译的路由URL,javascript,angularjs,Javascript,Angularjs,我正在尝试在angularJS应用程序中创建一个最近访问过的菜单,但我似乎找不到一个好方法来获取编译后的URLrouteChangeStart事件,它只是一个模板 我的代码如下所示: $rootScope.$on("$routeChangeStart", function (event, next, current) { if ($route.current && $route.current.$$route && $rou

我正在尝试在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>
我的模板如下所示:

       $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否,您只需缓存该值并在需要时使用它。