AngularJS - 根据当前路径更改 href 属性 [英] AngularJS - change href Attribute based on current path

查看:19
本文介绍了AngularJS - 根据当前路径更改 href 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据当前路径更改 href-Attribute 的值?

How can I change the value of the href-Attribute based on the current path?

我在 AngularJS 中的 if 子句如下所示:

My if clause in AngularJS looks like this:

    if (newPath.indexOf('test') > -1) {
      // change the value of the href-Attribute
    } else {
      // don't change the value of the href-Attribute
    }

我的 HTML 如下所示:

My HTML looks like this:

    <li>
      <a aria-expanded="false" role="button" href="/path1/path2/path4">Downloads</a>
    </li>

推荐答案

你可以使用 ng-href 动态给 href 赋值

you can use ng-href to dynamically assign value to href

<li>
  <a aria-expanded="false" role="button" ng-href="{{myVar}}">Downloads</a>
</li>

$scope.myVar ="/path1/path2/path4";

if (newPath.indexOf('test') > -1) {
      $scope.myVar = "/path1/path2/path4" // you can assign whatever path to this variable 
} else {
      $scope.myVar = "/path1/path2/path3" // you can assign whatever path to this variable
}

这篇关于AngularJS - 根据当前路径更改 href 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆