6

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?

4 Answers 4

22

You can try this

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

http://jsfiddle.net/54ema4k0/

Sign up to request clarification or add additional context in comments.

Comments

5

Just do like this its very simple.

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

Comments

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

Comments

0

Try conditional operator

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

or

set your link value in scope,

in your controller,

$scope.dynamic_url = 'somelink'

in view,

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

2 Comments

conditional operator does not work,it sends to " m ? "link1" : "link2" " in url
where you declare & define m scope ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.