首页 > 解决方案 > '/:routeparams' 正在覆盖 angularjs 中的其他路由

问题描述

app.config(function($routeProvider) {
    $routeProvider
   .when('/', {
       templateUrl : 'junk.html',
   })
   .    .when('/:pageuniqueid', {
       templateUrl : 'page.html',
   })
   // route for the about page
   .when('/first', {
       templateUrl : 'first.html',
   })

   // route for the contact page
   .when('/second', {
       templateUrl : 'second.html',
   });
});

如果我在 URL 中键入“example.com/first”,那么我得到的不是 first.html,而是 page.html。我正在实现用户可以在基本 URL 之后使用其动态 pageid 直接访问的页面。只有当它与其他路由不匹配时,我才想获取 page.html。有没有办法做到这一点?

标签: angularjs

解决方案


路由定义的顺序很重要。

如果您在 '/:pageuniqueid' 之前定义 '/first' 路由,它应该可以工作。

角度路由器在第一次匹配后停止评估路由。

因此,为了将 page.html 作为后备,您应该将其作为列表中的最后一个条目。


推荐阅读