5
基本上,我有这样的代码在我的模板:显示链接有条件Angular.js
<tr ng-repeat="entry in tableEntries">
<td ng-switch="entry.url == ''">
<span ng-switch-when="false"><a href="{{entry.url}}">{{entry.school}}</a></span>
<span ng-switch-when="true">{{entry.school}}</span>
</td>
...
</tr>
正如你可以看到,我想显示一个可点击的URL时entry.url
不是空的,一个纯文本,否则。它工作正常,但看起来相当难看。有没有更优雅的解决方案?
我能想到这样做的另一种方法是使用ng-if
:
<td>
<span ng-if="entry.url != ''"><a href="{{entry.url}}">{{entry.school}}</a></span>
<span ng-if="entry.url == ''">{{entry.school}}</span>
</td>
但我会被重复重复几乎相同的比较,它看起来更糟。你们如何处理这个问题?
这可能会帮助你:http://stackoverflow.com/questions/15810278/if-else-statement-in-angularjs-templates – Blauharley
使用'
回答
您可以试试。
<div ng-show="!link">hello</div> <div ng-show="!!link"><a href="{{link}}">hello</a></div>
但是,你正在使用应该罚款的
ngSwitch
。来源
2015-01-02 11:00:16
我建议有一个纳克级=“{‘的className’:whenEntryURLisWhatever}”在TD,并使其改变CSS样式访问,如:
然后,只需改变什么发生在在您的javascript代码中定义的函数内单击ng。
这将减少对代码的重复,为您的HTML现在可能是:
来源
2015-01-02 11:02:07
什么ng-click?我不知道你在说什么。你建议在CSS中隐藏未使用的元素吗?它现在比我现在做的更好吗? –
没有,在你的CSS你可以有'.className span {text-decoration:underline;光标:指针; ''和其他你想要的链接样式。然后在你的范围内,有'ng-click =“yourFunction()”' –
我已经编辑了答案,试着更好地解释这一点。 –
使用双重否定,把它转换成布尔从而
!!entry.url
将返回true
如果字符串不为空。很好看What is the !! (not not) operator in JavaScript?和Double negation (!!) in javascript - what is the purpose?
来源
2015-01-02 11:07:42 Satpal
您可以创建一个隐藏复杂的自定义指令:
HTML
JS
来源
2015-01-02 11:23:21 pixelbits
相关问题