Javascript ng-href 中的 AngularJs if 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29888624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJs if statement in ng-href
提问by user4773604
I have some elements which are created dynamically and every element has a different ng-href.
我有一些动态创建的元素,每个元素都有不同的 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.
当我尝试在 ng-href 中编写函数时,它会将页面发送到 url 中的函数,因此它不起作用。
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?
我应该使用哪种方法来创建具有不同 ng-href 的元素?
回答by FDisk
You can try this
你可以试试这个
<a ng-href="{{m ? '#linkOne':'#linkTwo'}}" ng-click="test(type)">your link</a>
回答by Jobincs
Just do like this its very simple.
这样做就很简单了。
<a href="{{variable == value ? '#link1' : '#link2'}}">Link</a>
回答by Arun P Johny
回答by ramamoorthy_villi
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>

