8

I'm getting Bool value from database to 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>

4 Answers 4

19
<td>
 {{true == patient.Alcoholic ? 'Yes' : 'No' }}
</td>

This should work!

Sign up to request clarification or add additional context in comments.

1 Comment

just use {{patient.Alcoholic ? 'Yes' : 'No' }}
11

use ng-if directive

 <td ng-if="patient.Alcoholic">Yes</td>
 <td ng-if="!patient.Alcoholic">NO</td>

3 Comments

Yours is a more appropriate answer.
Both working, one is ternary another is pure AngularJs, thanks :)
@Simply - Try to use angular way, Its my suggestion Thanks
2

Simply do

<td>
 {{ patient.Alcoholic ? 'Yes' : 'No' }}
</td>

Comments

1

Try to not put JS operations in your template, as it will:

  • Make your template dirty (imho).
  • Strain the application (very minor argument), as the evaluation is being run on each $digest cycle.

If you are fine with modifying the original bool of the patient:

$scope.patient.Alcoholic = !!$scope.patient.Alcoholic ? 'Yes' : 'No';

If not, I would add another property onto patient:

$scope.patient.isAlcoholic = !!$scope.patient.Alcoholic ? 'Yes' : 'No';

And then in your view (dependent on the solution you've chosen of the two above):

{{ patient.Alcoholic }}
<!-- or -->
{{ patient.isAlcoholic }}

That's my two cents on keeping your template clean.

1 Comment

Feel free to update the accepted answer and/or up it, if you found the given answer useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.