问题
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?
回答1:
You can try this
<a ng-href="{{m ? '#linkOne':'#linkTwo'}}" ng-click="test(type)">your link</a>
http://jsfiddle.net/54ema4k0/
回答2:
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:
Just do like this its very simple.
<a href="{{variable == value ? '#link1' : '#link2'}}">Link</a>
回答4:
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>
来源:https://stackoverflow.com/questions/29888624/angularjs-if-statement-in-ng-href