Javascript Angularjs - 将真/假显示为是/否
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26162509/
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
Angularjs - Show True/False as Yes/No
提问by SrAxi
Is there a simple way to show a true/false value as yes/no?
有没有一种简单的方法可以将真/假值显示为是/否?
I am retrieving from the Database a JSON that contains :
我正在从数据库中检索包含以下内容的 JSON:
[Object, Object, "WithCertification": true]
[对象,对象,“WithCertification”:true]
This is the html:
这是html:
With Certification {{elem.WithCertification}}
有认证{{elem.WithCertification}}
Is displaying this:
正在显示这个:
With Certification true
认证为真
I want it to display this:
我希望它显示这个:
With Certification yes
有认证 是
Is there a way without having to go to the controller to change the true/false to yes/no?
有没有办法不必去控制器将真/假更改为是/否?
Anyhow I need to change it, any suggestions?
无论如何我需要改变它,有什么建议吗?
Thank you.
谢谢你。
回答by Josep
You can have ternary operatorsinside Angular Expressions, so just do this:
您可以在Angular Expressions 中使用三元运算符,因此只需执行以下操作:
With Certification {{elem.WithCertification?'yes':'no'}}
回答by Evan Burbidge
You can use either a filter or a ternary operator. The filter would probably be better from what i've read ternary operators are frowned upon.
您可以使用过滤器或三元运算符。从我读过的三元运算符不赞成的内容来看,过滤器可能会更好。
app.filter('YesNo', function(){
return function(text){
return text ? "Yes" : "No";
}
})
The code is fairly simple set your app.filter, give it a name, inside pass an anon function, returning a function that takes your true,false as a param, then return the text and determine if true or false, if true return yes, otherwise no.
代码相当简单,设置你的 app.filter,给它一个名字,在里面传递一个匿名函数,返回一个将你的真假作为参数的函数,然后返回文本并确定是真还是假,如果真返回是,否则没有。
Alternatively do it inside the tag.
或者在标签内进行。
<td>{{item.value ? "Yes":"No"}}</td>
You can see this working here http://plnkr.co/edit/altgrjKhXHACmczOvGFw
你可以在这里看到这个工作http://plnkr.co/edit/altgrjKhXHACmczOvGFw
回答by Yordi Lorenzo Boeit Niet
{{elem.WithCertification == true ? "Yes" : "No"}}
回答by Abhishek Jain
The best way is to create a custom filter, as it is best to put as less logic in the view as possible. Below is a sample code showing you how to achieve this.
最好的方法是创建自定义过滤器,因为最好在视图中放置尽可能少的逻辑。下面是一个示例代码,向您展示如何实现这一点。
angular.module('myApp', [])
.filter('yesOrNo', function() {
return function(input) {
return input === 'true' ? 'yes' : 'no' ;
};
})
In html, you would just do:-
在 html 中,您只需执行以下操作:-
With Certification {{elem.WithCertification | yesOrNo}}
回答by Remco Haszing
If this is a one time case, I agree with Joseps answer. Use:
如果这是一次性案例,我同意Joseps 的回答。用:
With Certification {{elem.WithCertification?'yes':'no'}}
However, if you intend to use this more often, I would recommend using a filter. A very similar case is explained in the AngularJS tutorial step 9
但是,如果您打算更频繁地使用它,我建议您使用过滤器。AngularJS 教程第 9 步中解释了一个非常相似的案例

