0

現在のパスに基づいてhref-Attributeの値を変更するにはどうすればよいですか?AngularJS - hrefの変更現在のパスに基づく属性

マイAngularJSでWHERE句は次のように見える場合:

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> 

答えて

3

あなたは

<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 
} 
+0

作品href属性を交換し、あなたに感謝しない:) – Codehan25

+0

何の問題仲間をすることができます:あなたがそこにあなたのコードを記述する場合はD –

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> 
をてhrefに動的に値を代入する ng-hrefを使用することができます
+2

は、それは良い答えになります! – Deep

1

Y OUは

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

と現在のパスを取得する魔法のように

var myEl = angular.element(document.querySelector('#selector')); 
myEl.attr('href',url); 
関連する問題