Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Boolean values to text in angular 2 client side

Detail

I am trying to get data from api and show into the table on of the column contains status attribute which return true or false value but I want to show on client side Active or Block instead of this. How can I able to achieve this in angular 2.

enter image description here

like image 677
Malik Kashmiri Avatar asked Jul 12 '16 06:07

Malik Kashmiri


3 Answers

How can I able to achieve this in angular 2

Just use javascript/typescript:

const valueFromServer = false; // Assuming
const toShow = valueFromServer ? 'Active' : 'Blocked';
like image 147
basarat Avatar answered Sep 21 '22 18:09

basarat


I would suggest a pipe that returns either active or blocked according to the boolean.

import {Pipe, PipeTransform} from '@angular/core';

@Pipe({name: 'activeBlocked'})
export class ActiveBlockedPipe implements PipeTransform {
    transform(value) {
        return value ? 'Active' : 'Blocked';
    }
}

Then you can use it like this in you components template:

{{value | activeBlocked}}

In my opinion this is the easiest to reuse.

like image 29
Dinistro Avatar answered Nov 14 '22 02:11

Dinistro


You could do it directly in the view:

<td>{{user.IsActive && 'Active' || 'Block'}}</td>
like image 9
Des Horsley Avatar answered Nov 14 '22 01:11

Des Horsley