[英]AngularJS - change href Attribute based on current path
如何根據當前路徑更改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>
您可以使用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
}
您可以使用獲取當前路徑
var url = $location.absUrl().split('?')[0];
替換href屬性
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.