在 Angular.js 中有条件地显示 link
Displaying link conditionally in 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>
如您所见,我试图在 entry.url
不为空时显示可点击的 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>
但是我会重复几乎相同的比较两次,这看起来更糟。你们会如何处理这个问题?
你可以试试
<div ng-show="!link">hello</div>
<div ng-show="!!link"><a href="{{link}}">hello</a></div>
不过你用的ngSwitch
应该没问题。
我建议在你的 td 中使用 ng-class="{'className': whenEntryURLisWhatever}" ,并让它改变访问的 css 样式,例如:
td span{ /*#normal styles#*/ }
.className span{ /*#styles in the case of added classname (when it is a link)#*/
text-decoration: underline;
cursor: pointer;
}
然后只需在 javascript 代码中定义的函数中更改 ng-click 时发生的情况。
$scope.yourFunction = function(url){
if(!!url){
window.location = YourURL;
}
}
这会减少代码重复,因为您的 html 现在可以是:
<tr ng-repeat="entry in tableEntries">
<td ng-class="{'className': !!entry.url}">
<span ng-click="yourFunction(entry.url)">{{entry.school}}</span>
</td>
...
</tr>
使用双重否定,它转换为布尔值,因此如果字符串不为空,!!entry.url
将 return true
。
<td ng-switch="!!entry.url">
<span ng-switch-when="true"><a href="{{entry.url}}">{{entry.school}}</a></span>
<span ng-switch-when="false">{{entry.school}}</span>
</td>
读得不错What is the !! (not not) operator in JavaScript? and Double negation (!!) in javascript - what is the purpose?
您可以创建隐藏复杂性的自定义指令:
HTML
<tr ng-repeat="entry in tableEntries">
<td>
<link model="entry"></link>
</td>
...
</tr>
JS
app.directive('link', function() {
return {
restrict: 'E',
scope: {
model: '='
},
template: '<a ng-if="model.url != ''" href="{{model.url}}">{{model.school}}</a><span ng-if="model.url == ''"> {{ model.school }}</span>'
}
});
基本上,我的模板中有这段代码:
<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>
如您所见,我试图在 entry.url
不为空时显示可点击的 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>
但是我会重复几乎相同的比较两次,这看起来更糟。你们会如何处理这个问题?
你可以试试
<div ng-show="!link">hello</div>
<div ng-show="!!link"><a href="{{link}}">hello</a></div>
不过你用的ngSwitch
应该没问题。
我建议在你的 td 中使用 ng-class="{'className': whenEntryURLisWhatever}" ,并让它改变访问的 css 样式,例如:
td span{ /*#normal styles#*/ }
.className span{ /*#styles in the case of added classname (when it is a link)#*/
text-decoration: underline;
cursor: pointer;
}
然后只需在 javascript 代码中定义的函数中更改 ng-click 时发生的情况。
$scope.yourFunction = function(url){
if(!!url){
window.location = YourURL;
}
}
这会减少代码重复,因为您的 html 现在可以是:
<tr ng-repeat="entry in tableEntries">
<td ng-class="{'className': !!entry.url}">
<span ng-click="yourFunction(entry.url)">{{entry.school}}</span>
</td>
...
</tr>
使用双重否定,它转换为布尔值,因此如果字符串不为空,!!entry.url
将 return true
。
<td ng-switch="!!entry.url">
<span ng-switch-when="true"><a href="{{entry.url}}">{{entry.school}}</a></span>
<span ng-switch-when="false">{{entry.school}}</span>
</td>
读得不错What is the !! (not not) operator in JavaScript? and Double negation (!!) in javascript - what is the purpose?
您可以创建隐藏复杂性的自定义指令:
HTML
<tr ng-repeat="entry in tableEntries">
<td>
<link model="entry"></link>
</td>
...
</tr>
JS
app.directive('link', function() {
return {
restrict: 'E',
scope: {
model: '='
},
template: '<a ng-if="model.url != ''" href="{{model.url}}">{{model.school}}</a><span ng-if="model.url == ''"> {{ model.school }}</span>'
}
});