AngularJs if statement in ng-href

31,201

Solution 1

You can try this

<a ng-href="{{m ? '#linkOne':'#linkTwo'}}" ng-click="test(type)">your link</a>

http://jsfiddle.net/54ema4k0/

Solution 2

Just do like this its very simple.

<a href="{{variable == value ? '#link1' : '#link2'}}">Link</a>

Solution 3

You need to use a variable for the link

<a ng-href='{{link}}' ng-click='test(type);'> your link </a>

then

$scope.$watch('m', function(value){
    $scope.link = value ? 'link1': 'link2';
})

Demo: Fiddle

Share:
31,201
user4773604
Author by

user4773604

Updated on April 17, 2020

Comments

  • user4773604
    user4773604 about 4 years

    I have some elements which are created dynamically and every element has a different ng-href.

    I want to give different link according to some elements.

    When I try to write function in ng-href it sends the page to function in url,therefore it does not work.

    I try to do something like this;

           .......
                 <a 
                ng-href='if(m){#linkOne} else {#linkTwo}'
                ng-click='test(type);'>
    
                  </a> 
           .......
    

    Which method should i use to create element with different ng-href?

  • user4773604
    user4773604 almost 9 years
    conditional operator does not work,it sends to " m ? "link1" : "link2" " in url
  • ramamoorthy_villi
    ramamoorthy_villi almost 9 years
    where you declare & define m scope ?