2015-02-11 30 views
1

我有angularJS非常新手问题:有条件地追加字符串角JS干净的方式来标记属性

我有两个<a>标签非常相似,只显示如果变量foo设置其href取决于:

检查foo == NULL:

<a ng-href="#/bar/{{col.slug}}">{{col.title}}</a> 

其他

<a ng-href="#/{{foo}}/bar/{{col.slug}}">{{col.title}}</a> 

正如您所看到的,除了url的开头附加的foo变量之外,标签非常相似。

我知道有像ng-showng-hide,ng-ifternary operation这样的命令。

如何以最清洁的方式进行此操作?

谢谢!

回答

3

你可以调节你的代码:

<a ng-href="#/{{ foo != null ? foo + '/' : ''}}bar/{{col.slug}}">{{col.title}}</a> 

或者你可以使用ngShow:

<a ng-show="foo == null" ng-href="#/bar/{{col.slug}}">{{col.title}}</a> 
    <a ng-show="foo != null" ng-href="#/{{foo}}/bar/{{col.slug}}">{{col.title}}</a> 

我更喜欢在线状态,发现它更redable

0

的数据小于发送到浏览器越好。

<a ng-href="#/{{ !!foo ? foo + '/' : '/'}}bar/{{col.slug}}">{{col.title}} 
相关问题