英文:
Angular - Name with title base on case
问题
我在Angular中有以下数据:
data = [
{"id": "1", "firstname": "Test", "lastname": "Test2", "sex": "Female"},
{"id": "2", "firstname": "fname", "lastname": "lname", "sex": "Male"},
{"id": "3", "firstname": "Rajesh", "lastname": "Singh", "sex": "Male"},
{"id": "4", "firstname": "Sim", "lastname": "Sen", "sex": "Female"}
]
我希望输出结果如下:
Mrs. Test Test2
Mr. fname lname
Mr. Rajesh Singh
Mrs. Sim Sen
我的代码是:
<ul>
<li *ngFor="let d of data">
{{ d.firstname }} {{d.lastname}}
</li>
</ul>
请告诉我如何将性别转换为先生和女士。
我是Angular的新手。
请帮忙。
英文:
I have the following data in Angular:
data = [
{"id": "1", "firstname": "Test", "lastname": "Test2", "sex": "Female"},
{"id": "2", "firstname": "fname", "lastname": "lname", "sex": "Male"},
{"id": "3", "firstname": "Rajesh", "lastname": "Singh", "sex": "Male"},
{"id": "4", "firstname": "Sim", "lastname": "Sen", "sex": "Female"}
]
I want the output as:
> Mrs. Test Test2
>
> Mr. fname lastname
>
> Mr. Rajesh Singh
>
> Mrs. Sim Sen
My Code:-
<ul>
<li *ngFor="let d of data">
{{ d.firstname }} {{d.lastname}}
</li>
</ul>
Please tell me how can I convert the gender to Mr and Mrs.
I am new to Angular.
Please help.
答案1
得分: 2
有多种方法可以实现:
解决方案1: 在视图中使用三元条件运算符来显示标题。
<ul>
<li *ngFor="let d of data">
{{ d.sex == 'Male' ? '先生' : d.sex == 'Female' ? '女士' : '' }} {{ d.firstname }} {{d.lastname}}
</li>
</ul>
解决方案2: 在data
数组的每个对象中添加title
属性。
this.data = this.data.map((x) => ({
...x,
title: x.sex == 'Male' ? '先生' : x.sex == 'Female' ? '女士' : '',
}));
<ul>
<li *ngFor="let d of data">
{{ d.title }} {{ d.firstname }} {{d.lastname}}
</li>
</ul>
解决方案3: 实现Angular管道
您可以通过传递sex
值来创建一个Angular管道来处理标题。这种方法会提供更好的性能,因为Angular管道会缓存管道的结果。
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'title'
})
export class TitlePipe implements PipeTransform {
transform(value: any, args?: any): any {
return value == 'Male' ? '先生' : value == 'Female' ? '女士' : '';
}
}
确保在使用之前将管道导入应用程序模块。
<ul>
<li *ngFor="let d of data">
{{ d.sex | title }} {{ d.firstname }} {{d.lastname}}
</li>
</ul>
英文:
Plenty of ways you can achieve:
Solution 1: Implement the ternary conditional to display title in the view.
<ul>
<li *ngFor="let d of data">
{{ d.sex == 'Male' ? 'Mr.' : d.sex == 'Female' ? 'Mrs.' : '' }} {{ d.firstname }} {{d.lastname}}
</li>
</ul>
Solution 2: Add the title
properties in each object for the data
array.
this.data = this.data.map((x) => ({
...x,
title: x.sex == 'Male' ? 'Mr.' : x.sex == 'Female' ? 'Mrs.' : '',
}));
<ul>
<li *ngFor="let d of data">
{{ d.title }} {{ d.firstname }} {{d.lastname}}
</li>
</ul>
Solution 3: Implement Angular pipe
You can create an Angular pipe for title by passing the sex
value. This approach will provide a better performance as Angular pipe will cache the pipe result.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'title'
})
export class TitlePipe implements PipeTransform {
transform(value: any, args?: any): any {
return value == 'Male' ? 'Mr.' : value == 'Female' ? 'Mrs.' : '';
}
}
Make sure that you have imported the pipe into the app module before using it.
<ul>
<li *ngFor="let d of data">
{{ d.sex | title }} {{ d.firstname }} {{d.lastname}}
</li>
</ul>
答案2
得分: 1
你可以通过使用Angular的插值{{ }}
和条件(三元)运算符来实现这一点。该运算符可以根据性别渲染正确的标题。以下是如何修改你的代码:
<ul>
<li *ngFor="let d of data">
{{ d.sex === 'Male' ? '先生' : '女士' }} {{ d.firstname }} {{ d.lastname }}
</li>
</ul>
英文:
You can achieve this by using Angular's interpolation {{ }}
with a conditional (ternary) operator. This operator can be used to render the correct title based on the gender. Here's how you can modify your code:
<ul>
<li *ngFor="let d of data">
{{ d.sex === 'Male' ? 'Mr.' : 'Mrs.' }} {{ d.firstname }} {{ d.lastname }}
</li>
</ul>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论