AngularJS - 根据当前路径更改 href 属性
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
}
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>
您可以通过
获取当前路径
var url = $location.absUrl().split('?')[0];
替换 href 属性
var myEl = angular.element( document.querySelector( '#selector' ) );
myEl.attr('href',url);
如何根据当前路径更改 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
}
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>
您可以通过
获取当前路径var url = $location.absUrl().split('?')[0];
替换 href 属性
var myEl = angular.element( document.querySelector( '#selector' ) );
myEl.attr('href',url);