我有一个CMS,可强制将URL指向特定模式的页面。我们需要有条件地重写这些链接的href。
CMS将在页面上打印以下内容:
<a href="/path/to/the/zoo/gorilla.html">Go</a>
我们的路由器需要实际指向
#/zoo/gorilla
如果我们自己编写了此链接,它将看起来像:
<a ui-sref="zoo('gorilla')>Go</a>
问题是,我们不能总是保证/ zoo / gorilla部分意味着我们在zoo('gorilla')。
最简单的方法是将CMS URL解析为路由器URL,然后执行以下操作:
link.attr("href", "#/zoo/gorilla");
我知道为什么这通常与ui-router的想法背道而驰,但是我希望找到一种在这种奇怪情况下使用它的方法。
我认为这是可行的,基本上是一条指令,可用来更改您单击时指向哪个URL。
module.directive('newHref', ['$location',function($location) {
return function(scope, element, attrs) {
element.bind('click', newHref);
function newHref(event) {
var hrefList = event.target.href.split("/");
var parent = hrefList[hrefList.length-2];
var child = hrefList[hrefList.length-1].split(".")[0];
$location.path("/" + hrefList.slice(0, hrefList.length-2).join("/") + "/" + parent + "/" + child);
}
}
}]);
像这样使用它:
<a href="/path/to/the/zoo/gorilla.html" new-href>Go</a>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句