Ternary operator with variable substitution in HTML

时间:2017-08-04 13:10:18

标签: html angularjs

In a side panel of an app I want to either display "enabled" or "disabled" and I know I can write two different ng-if statements, but I wanted to see if there was a clever way to do it in just single line.

For reference here is what I have that works right now:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="rightColumn" ng-if="selectedProject.backupEnabled">Enabled</div>
<div class="rightColumn" ng-if="!selectedProject.backupEnabled">Disabled</div>

This isn't super critical obviously, but would be neat to know more about

1 个答案:

答案 0 :(得分:5)

<div class="rightColumn">{{ selectedProject.backupEnabled ? 'Enabled' : 'Disabled' }}</div>

This will work.