admin管理员组文章数量:1415119
I am trying to apply a condition to href or ng-href.
The condition is if email !== null
My code looks like this:
<a ng-attr-href="{{email !== null}}" href="mailto:{{email | lowercase}}">{{email | lowercase | nullValue}}</a>
This is evaluating to either href="true"
or href="false"
.
This seems close to working, but I still get the href attribute being there if the value is null:
ng-attr-href="mailto:{{email !== null ? email: email | lowercase}}"
How do I remove the href entirely if the value from the data is null? Is it possible to wrap the entire condition around the html href attribute?
I am trying to apply a condition to href or ng-href.
The condition is if email !== null
My code looks like this:
<a ng-attr-href="{{email !== null}}" href="mailto:{{email | lowercase}}">{{email | lowercase | nullValue}}</a>
This is evaluating to either href="true"
or href="false"
.
This seems close to working, but I still get the href attribute being there if the value is null:
ng-attr-href="mailto:{{email !== null ? email: email | lowercase}}"
How do I remove the href entirely if the value from the data is null? Is it possible to wrap the entire condition around the html href attribute?
Share Improve this question asked Sep 24, 2015 at 11:35 lharbylharby 3,2776 gold badges26 silver badges65 bronze badges2 Answers
Reset to default 4There are several ways of solving your problem. I prefer this:
<a ng-if="email !== null" href="mailto:{{email | lowercase}}">{{email | lowercase | nullValue}}</a>
<a ng-if="email == null">{{email | lowercase | nullValue}}</a>
You can easily wrap this into a directive if you need more plex conditions or attribute toggles.
You can also use something similar to your ng-attr-href as before but instead with only ng-href :
<a ng-href="{{email ? 'mailto:'+(email | lowercase) : ''}}">
{{email | lowercase | nullValue}}
</a>
While this would always create the ng-href attribute in the anchor tag, without an email it would be empty and would not create the href attribute. The end results would look like the following:
<a ng-href="mailto:tests" href="mailto:tests">tests</a>
<a ng-href></a>
本文标签: javascriptRemove href attribute if value is null in angularjsStack Overflow
版权声明:本文标题:javascript - Remove href attribute if value is null in angularjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745229048a2648741.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论