删除Angularjs中的尾部斜杠

时间:2016-09-19 10:32:37

标签: angularjs

我在其他一个堆栈溢出问题中找到了这个代码段。

$urlRouterProvider.rule(function($injector, $location){
  let path = $location.path();
  let hasTrailingSlash = path[path.length-1] === '/';
  if(hasTrailingSlash) {
    console.log('coming in the has trailing slash rule function');
    let normalizedPath = path.substr(0, path.length - 1);
    $location.replace().path(normalizedPath);
  }
});

我有以下表格中的一些网址/school/:id/school/:id/class/:id

如果我在/class/121/格式中输入网址并将其重定向到/class/121,同样/about-us/重定向到/about-us,则上述功能完全正常。但是当我输入/school/时,我收到404错误。根据这个https://github.com/angular-ui/ui-router/wiki/url-routing

  

' /用户/:ID' - 匹配' / user / bob'或者' / user / 1234 !!!'甚至' / user /'但不是' / user'或' / user / bob / details'。第二个路径段将被捕获为参数' id'

所以我猜它会进入/school/:id状态,请注意,在这种情况下,$urlRouterProvider.rule不会被调用。一些示例定义的模块

.config(function($stateProvider) {
    'ngInject';
    $stateProvider
        .state('school', {
    hasLefSideMenu: true,

            url: '/school/:id',
            templateUrl: '/school/school.html',
            controller: 'SchoolController',
            reloadOnSearch: false,
            controllerAs: 'schoolCtrl'
        });
})



.config(function($stateProvider) {
'ngInject';

$stateProvider
  .state('all-school', {
    hasLefSideMenu: true,

    url: '/school',
    templateUrl: '/all-schools.html',
    controller: 'allSchoolsController',
    controllerAs: 'allSchoolsCtrl'
  });

})

理想情况下,对于所有网址,应删除斜杠。任何帮助都会很棒

1 个答案:

答案 0 :(得分:0)

您可以使用

$urlMatcherFactoryProvider.strictMode(false)

描述了here

相关问题