Arjun
Arjun

Reputation: 516

AngularJS Print Yes or No based on boolean value

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>

Upvotes: 8

Views: 12153

Answers (4)

Gurmeet Khalsa
Gurmeet Khalsa

Reputation: 136

Simply do

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

Upvotes: 2

user1364910
user1364910

Reputation:

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.

Upvotes: 1

Coder John
Coder John

Reputation: 786

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

This should work!

Upvotes: 19

Sudharsan S
Sudharsan S

Reputation: 15393

use ng-if directive

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

Upvotes: 11

Related Questions