Javascript AngularJS-根据当前路径更改href属性
如何根据当前路径更改href属性的值 AngularJS中的if子句如下所示:Javascript AngularJS-根据当前路径更改href属性,javascript,angularjs,twitter-bootstrap,angularjs-scope,angularjs-ng-href,Javascript,Angularjs,Twitter Bootstrap,Angularjs Scope,Angularjs Ng Href,如何根据当前路径更改href属性的值 AngularJS中的if子句如下所示: if (newPath.indexOf('test') > -1) { // change the value of the href-Attribute } else { // don't change the value of the href-Attribute } <li> <a aria-expanded="fal
if (newPath.indexOf('test') > -1) {
// change the value of the href-Attribute
} else {
// don't change the value of the href-Attribute
}
<li>
<a aria-expanded="false" role="button" href="/path1/path2/path4">Downloads</a>
</li>
我的HTML如下所示:
if (newPath.indexOf('test') > -1) {
// change the value of the href-Attribute
} else {
// don't change the value of the href-Attribute
}
<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
}
$scope.myVar=“/path1/path2/path4”;
if(newPath.indexOf('test')>-1){
$scope.myVar=“/path1/path2/path4”/
}否则{
$scope.myVar=“/path1/path2/path3”/
}
您可以使用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
}
$scope.myVar=“/path1/path2/path4”;
if(newPath.indexOf('test')>-1){
$scope.myVar=“/path1/path2/path4”/
}否则{
$scope.myVar=“/path1/path2/path3”/
}
if(newPath.indexOf('test')>-1){
$scope.newPath=newPath.test;
}否则{
//不要更改href属性的值
}
if(newPath.indexOf('test')>-1){
$scope.newPath=newPath.test;
}否则{
//不要更改href属性的值
}
您可以使用
var url = $location.absUrl().split('?')[0];
替换href属性
var myEl = angular.element( document.querySelector( '#selector' ) );
myEl.attr('href',url);
您可以使用获取当前路径
var url = $location.absUrl().split('?')[0];
替换href属性
var myEl = angular.element( document.querySelector( '#selector' ) );
myEl.attr('href',url);
如果您在那里描述您的代码,这将是一个更好的答案!如果您在那里描述您的代码,这将是一个更好的答案!