typescript 将布尔值更改为 angular 2 客户端中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38321634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Boolean values to text in angular 2 client side
提问by Malik Kashmiri
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.
我正在尝试从 api 获取数据并显示到包含返回 true 或 false 值的状态属性的表中,但我想在客户端显示 Active 或 Block 而不是这个。我怎样才能在角度 2 中实现这一点。
回答by Dinistro
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.
在我看来,这是最容易重用的。
回答by Des Horsley
You could do it directly in the view:
您可以直接在视图中执行此操作:
<td>{{user.IsActive && 'Active' || 'Block'}}</td>
回答by basarat
How can I able to achieve this in angular 2
我怎样才能在角度 2 中实现这一点
Just use javascript/typescript:
只需使用 javascript/打字稿:
const valueFromServer = false; // Assuming
const toShow = valueFromServer ? 'Active' : 'Blocked';
回答by Rajitha Alluri
Simple way is use conditional operator inside td element
简单的方法是在 td 元素中使用条件运算符
<td>{{user.status ? 'Active' : 'Block'}}</td>