在我们的应用程序中,我们在foobar.com/public/angular之外提供静态的角度应用程序文件。主页(foobar.com)配置为服务于foobar.com/public/angular/index.html。因此,在索引文件的<head>中,我们有:
<base href="http://foobar.com/public/angular">但是,在开发过程中,我们使用foobar.com/debug,这会导致服务器使用以下内容:
<base href="http://foobar.com/public/angular-debug">服务器从这个url返回未经处理的(不缩小、丑陋等)版本的文件。我们也在使用角度路线作为我们的观点。
TLDR:
foobar.comfoobar.com/debug<base href="http://foobar.com/public/whatever">问题是在客户端生成urls。正如注释中所讨论的那样,<a href="#/new/route">Click here</a>无法工作,因为生成的url包含基本标记,服务器拒绝这个标记,并且看起来很奇怪,因为用户在其他地方启动:foobar.com/public/angular/#/myRoute)。我认为创建所需的url的最好方法是:
/**
* Returns current url after replacing everything after the hashbang with a new string.
*
* @param {string} s - string to change route with (e.g. 'asdf')
* @return {string} - new url e.g. http://google.com/foobar/#!/asdf
*/
makeRouteUrl = function(s) {
var root = $location.absUrl().match(/.*#!/);
if (root && (root.length > 0)) {
root = root[0];
return( root + s );
} else {
// no hashbang in path? Choose home page (not intended use)
return "/";
}
}然而,这似乎有点恶心。,是否有一个角度上的函数使上述函数变得不必要?我已经在$location、$route等中寻找了函数,但是似乎没有什么能正确地处理复杂的设置。我们最终需要以字符串的形式获取url,而不是导航(我们支持在新选项卡中打开,因此需要使用ng-href)。由于角应用程序经常使用hashbang urls进行路由,所以我想一定有一些东西可以让您在hashbang之后更改路由,同时保留前面的内容。
发布于 2015-02-24 23:28:54
我不太确定我是否理解这个问题..。
$location.url('/stuff/after/hash?params=hey');或者,如果您只想在#之后设置路径:
$location.path('/stuff/after/hash');这是我通常使用的。但在此之后,您需要返回没有散列的url吗?怎么样
window.location.origin + window.location.pathname对于没有散列和参数的当前url?
发布于 2015-02-24 23:09:14
您考虑过使用UI路由器库吗?
总的来说,它很棒。
针对您的问题,UrlMatcher服务提供了一种format方法--除非我误解了您的要求--做您想做的事情。
new UrlMatcher('/your/route/here').format(optionalNamedParameters);
// returns a formatted URLhttps://stackoverflow.com/questions/28708097
复制相似问题