AngularJs if statement in ng-href

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

I want to give different links according to some elements.

When I try to write a function in ng-href, it sends the page to work in url, so it doesn't work.

I am trying 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 an element with different ng-href?

+3


source to share


4 answers


You can try this

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

      



http://jsfiddle.net/54ema4k0/

+15


source


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

+3


source


Just make it very easy.

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

      

+2


source


Try the conditional operator

<a ng-href='m ? "link1" : "link2" ' ng-click='test(type);'> your link </a>

      

or

set the value of your link in scope,

in your controller,

$scope.dynamic_url = 'somelink'

      

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

      

0


source







All Articles