AngularJS:ui-router href重写为ui-sref

oy屋

我有一个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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

AngularJS ui-router:填写href值时,ui-sref链接不位于哈希之前

angularjs ui-router默认子状态和ui-sref

AngularJS ui-router-在ui-sref中使用带有参数的过滤器?

Angular-ui-router:ui-sref-active与子活动

Angular-UI-Router:ui-sref不使用参数构建href

使用 ui-router 使用 <a href="/#/xyz> 而不是 <a ui-sref="primary"> 更改状态

AngularJS ui-sref链接卡住了

使用ui-router服务在离子框架中使用ui-sref和href(在何处使用)之间的区别

AngularJS ui路由器:测试ui-sref

将html插入div中,并保持ui-router ui-sref属性正常工作

角度ui.router ui-sref替换URL字符-美化

如何使用ui-router中的ui-sref将参数传递给控制器

从angular-ui-router中的ui-sref更改当前状态下的url参数

Angular-ui-router:ui-sref-active和嵌套状态

使用Ui-Router的ui-sref Just设置查询参数

如何获取ui-router的ui-sref指向嵌套状态?

Angular ui-router ui-sref-opts更新地址栏

Angular UI-Router ui-sref忽略一些元素

Angular UI-Router $ urlRouterProvider。当我单击<a ui-sref="...">时不起作用

angularjs ui.router

混合ui-sref和$ state.go以在Angular ui-router中进行状态转换

使用 ui-router-ng2 在 Angular 2 中不使用 ui-sref 更改状态。(Angular2 & UI-Router-ng2)

在href和ui-sref之间切换

使用 ui-sref 时不会自动生成 href

Angular ui-sref href解析完成回调

离子+角ui-sref不会产生href

根据条件禁用ui-sref

使用ui-sref传递数据属性

角度ui-sref编码参数