0

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

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

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

我的 HTML 如下所示:

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

3 回答 3

4

您可以使用ng-href为 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
}
于 2017-03-20T09:25:04.300 回答
1

您可以通过以下方式获取当前路径

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

替换 href 属性

var myEl = angular.element( document.querySelector( '#selector' ) );
myEl.attr('href',url);
于 2017-03-20T09:33:05.690 回答
0
 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>
于 2017-03-20T09:25:17.297 回答