javascript - AngularJS - change href Attribute based on current path - Stack Overflow

How can I change the value of the href-Attribute based on the current path?My if clause in AngularJS lo

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

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
    }

My HTML looks like this:

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

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

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
    }

My HTML looks like this:

    <li>
      <a aria-expanded="false" role="button" href="/path1/path2/path4">Downloads</a>
    </li>
Share asked Mar 20, 2017 at 9:18 Codehan25Codehan25 3,01412 gold badges59 silver badges109 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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
}

You can get current path with

var url = $location.absUrl().split('?')[0];

Replace href attribute

var myEl = angular.element( document.querySelector( '#selector' ) );
myEl.attr('href',url);
 if (newPath.indexOf('test') > -1) {
      $scope.newPath = newPath.test;
    } else {
      // don't change the value of the href-Attribute
    }

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744883130a4598958.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信