AngularJs if statement in ng-href

泪湿孤枕 提交于 2019-12-22 09:45:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!