简体   繁体   中英

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.

在此输入图像描述

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.

How can I able to achieve this in angular 2

Just use javascript/typescript:

const valueFromServer = false; // Assuming
const toShow = valueFromServer ? 'Active' : 'Blocked';

您可以直接在视图中执行此操作:

<td>{{user.IsActive && 'Active' || 'Block'}}</td>

简单的方法是在td元素中使用条件运算符

<td>{{user.status ? 'Active' : 'Block'}}</td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM