Zabs Zabs - 1 month ago 5x
AngularJS Question

How to refactor this logic in the template/view to use a single line if statement (AngularJS 1.x)

I am fixing some small issues within our AngularJS application - I've come across the logic below which simply displays one link.

In pseudocode style..

if (data.serviceId exists in the DOM) {
display link & populate the href from data.serviceId value
} elseif(commentReply.sender.serviceId exists in the DOM) {
display the link & populate the href from commentReply.sender.serviceId value

The code itself in the template looks as follows, how can I amend the code below so it is cleaner and I am not duplicating the line using some form of single line tertiary statement?

<a ng-if="data.serviceId" ng-href="/#/profile/{{data.serviceId}}">View</a>
<a ng-if="commentReply.sender.serviceId" ng-href="/#/profile/{{commentReply.sender.serviceId}}">View</a>


for example:

<a data-ng-href="/#/profile/{{data.serviceId && !commentReply.sender.serviceId ? data.serviceId : commentReply.sender.serviceId}}">View</a>


<a href="/#/profile/{{data.serviceId && !commentReply.sender.serviceId ? data.serviceId : commentReply.sender.serviceId}}">View</a>