Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS-根据当前路径更改href属性_Javascript_Angularjs_Twitter Bootstrap_Angularjs Scope_Angularjs Ng Href - Fatal编程技术网

Javascript AngularJS-根据当前路径更改href属性

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

如何根据当前路径更改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="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);
    

    如果您在那里描述您的代码,这将是一个更好的答案!如果您在那里描述您的代码,这将是一个更好的答案!