如何在 template/view 中重构此逻辑以使用单行 if 语句 (AngularJS 1.x)

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

我正在解决 AngularJS 应用程序中的一些小问题 - 我遇到了下面的逻辑,它只显示 one link.

伪代码风格..

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
}

模板中的代码本身如下所示,我如何修改下面的代码使其更清晰并且我没有使用某种形式的单行三级语句复制行?

<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>

显示一个 link 并从控制器获取您的 href 值:

<a ng-href="/#/profile/{{ myCtrl.getServiceIdHref() }}">View</a>

然后向您的控制器添加一个函数(按照您的伪代码):

// inside myCtrl.js

this.getServiceIdHref = function() {
    if (data.serviceId exists in the DOM) {
        return data.serviceId value
    } elseif(commentReply.sender.serviceId exists in the DOM) {
        return commentReply.sender.serviceId value
    }
}

例如:

<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>