AngularJS Print Yes or No based on boolean
I am getting Bool value from database in angularJs
<td>
{{patient.Alcoholic}}
</td>
instead of false or ture I need to print YES or NO
<td>
{{patient.Alcoholic // can i have if-else condition over here ??}}
</td>
+3
Arjun
source
to share
4 answers
<td>
{{true == patient.Alcoholic ? 'Yes' : 'No' }}
</td>
It should work!
+11
Coder john
source
to share
use the ng-if directive
<td ng-if="patient.Alcoholic">Yes</td>
<td ng-if="!patient.Alcoholic">NO</td>
+8
Sudharsan S
source
to share
Try not to put JS operations in your template as this will:
- Make your template dirty (imho).
- Extract the application (very minor argument) as the evaluation is done in every loop
$digest
.
If you are ok with modifying the bool
patient's original :
$scope.patient.Alcoholic = !!$scope.patient.Alcoholic ? 'Yes' : 'No';
If not, I would add another property to patient
:
$scope.patient.isAlcoholic = !!$scope.patient.Alcoholic ? 'Yes' : 'No';
And then in your opinion (depending on the solution you chose from the two above):
{{ patient.Alcoholic }}
<!-- or -->
{{ patient.isAlcoholic }}
That's my two cents for keeping your template clean.
+1
Kasper Lewau
source
to share
Just do
<td>
{{ patient.Alcoholic ? 'Yes' : 'No' }}
</td>
0
Gurmeet khalsa
source
to share