AngularJS - get the list of defined routes - $routeProvider

烈酒焚心 提交于 2019-12-05 02:09:34

turns out this is pretty straight forward:

http://plunker.co/edit/GNZxcvK4hfQ9LrlSvasK?p=preview

Components.filter('url', function ($route) {
  function resolveRoute(options, route) {
    var parts = route.split('/');
    for (var i = 0; i < parts.length; i++) {
      var part = parts[i];
      if (part[0] === ':') {
        parts[i] = options[part.replace(':', '')];
        if (parts[i] == undefined) throw Error('Attribute \'' + part + '\' was not given for route \'' + route + '\'')
      }
    }
    return parts.join('/');
  }

  return function (options, routeName) {
    var routes = [];


    angular.forEach($route.routes,function (config,route) {
      if(config.name===routeName){
        routes.push(route);
      }
    });

    if (routes.length == 1) {
      return resolveRoute(options, routes[0]);
    }
    else if (routes.length == 0) {
      throw Error('Route ' + routeName + ' not found');
    }
    throw Error('Multiple routes matching ' + routeName + ' were found');
  }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!