ngRoute只对路由配置中的链路使用

Use ngRoute only on links in route config

本文关键字:链路 配置 路由 ngRoute      更新时间:2023-09-26

假设我配置了两条路由,还有一个带有单独控制器的选项。

$routeProvider.
when('/careers/all', {
    title : 'All Careers',
    templateUrl: '/_get/html/listing,careers_app?action=all',
    controller: 'careersController',
    animation: 'slide'
}).
when('/careers/view/:job*', {
    title : '',
    controller: 'careersController',
    animation: 'slide'
}).otherwise({
    template : ' ',
    controller : 'redirectController'
});
// use the HTML5 History API
$locationProvider.html5Mode(true);

我只是想要一些东西来处理其他一切,以便它作为一个标准的锚会表现,我看到其他人已经添加了一个target="_self"到锚来让这个工作,但这是完全不切实际的,因为我有数百个锚,我不是唯一一个管理这个网站。我需要让这个控制器重定向,但显然不是连续循环因为页面刷新,然后触发else,然后页面再次刷新,所以我创建了一个递归刷新。

控制器:

_core.controller('redirectController', ['$location', '$window', '$rootScope', function($location, $window, $rootScope) {
    var path = $location.path();
    $window.location.href = path;
}]);

我能做些什么来设置这个,有没有办法在路由路径名设置之前检测?我知道有一个事件$routeChangeStart,但是路径在这个回调运行之前已经设置好了。

想法?

我想这就是你要找的。根据我的理解,你假装保持锚的正常行为,同时仍然保持推送状态工作。

这是我的方法:http://jsbin.com/kafoxu/1/edit

希望对你有所帮助

我通过完全删除else选项来实现这一点。并在$location服务中添加一个事件侦听器。

$rootScope.$on('$locationChangeStart', function (event, newUrl, oldUrl){
    var hasCareers = newUrl.indexOf("careers/") > -1;
    if (newUrl == oldUrl || hasCareers) return;
    $window.location.href = newUrl;
});

看到我只有两个路由,都包含职业/在url中,我只是做了一个检查,看看它是职业页面,或者我们已经在页面上,它试图改变,所以不要更新,否则触发刷新