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

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

问题描述

如何根据当前路径更改href属性的值?

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天全站免登陆