Angular Filter for returning true false from api?

So i am trying to display, weather a user is active or not active. The data comes via an api in Boolean form. So it will simply say user is true or user is false .I want to show 'Active' if data returns true, 'Not Active' if user return false.Solution i did was a simple if else that does the job, but this is heavily reused through out the app and i want to make an angular filter to do this task. I am a beginner in angular and java script and haven't created a filter before.

this is my if else and showing it in html this i how i want to use eventuatlly

<label>{{ $ctrl.myValu | truefalse}}</label>

  $onInit = () => {
        let self = this;

        this.siUser.current().then((userData) => {
            let actingAsTenant = userData.user.apikey;

            if (actingAsTenant) {

                self.siTenant.current().then((tenant) => {
                    self.tenant = tenant;
                    if ( == true) {
               = 'Active'
                    else if ( == false){
               = 'Not Active'
                }, (err) => {

        }, (err) => {
            self.siAlertDialog.error('Error rendering tenant details');

2 answers

  • answered 2017-06-17 18:11 Sumit Ridhal

    You can use Ternary Operator:


 == true ? 'Active' : 'Not Active';


    In Angular Expression

    <strong>{{ $ ? 'Active' : 'Not Active' }}</strong>

  • answered 2017-06-17 18:11 Arun

    For this you need to create filter. Try to with the given code
    <!DOCTYPE html>
        <html lang="en">
            <title>Ristorante Con Fusion: Menu</title>
              <script src=""></script>
          <div ng-app="myapp" ng-controller="userController" >
          {{ myValu | truefalse }}
       var app = angular.module('myapp', []);
       app.filter('truefalse', function(){
            return function(text){
              return text ? "Active" : "Not Active";
    app.controller('userController', function($scope) {
          $scope.myValu = true;