Angular – 根据情况命名带有标题的名称

huangapple go评论75阅读模式
英文:

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 = [
  {&quot;id&quot;: &quot;1&quot;, &quot;firstname&quot;: &quot;Test&quot;, &quot;lastname&quot;: &quot;Test2&quot;, &quot;sex&quot;: &quot;Female&quot;},
  {&quot;id&quot;: &quot;2&quot;, &quot;firstname&quot;: &quot;fname&quot;, &quot;lastname&quot;: &quot;lname&quot;, &quot;sex&quot;: &quot;Male&quot;},
  {&quot;id&quot;: &quot;3&quot;, &quot;firstname&quot;: &quot;Rajesh&quot;, &quot;lastname&quot;: &quot;Singh&quot;, &quot;sex&quot;: &quot;Male&quot;},
  {&quot;id&quot;: &quot;4&quot;, &quot;firstname&quot;: &quot;Sim&quot;, &quot;lastname&quot;: &quot;Sen&quot;, &quot;sex&quot;: &quot;Female&quot;}
]

I want the output as:

> Mrs. Test Test2
>
> Mr. fname lastname
>
> Mr. Rajesh Singh
>
> Mrs. Sim Sen

My Code:-

&lt;ul&gt;
    &lt;li *ngFor=&quot;let d of data&quot;&gt;
      {{ d.firstname }}  {{d.lastname}}
    &lt;/li&gt;
&lt;/ul&gt;

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>

在 StackBlitz 上查看演示

英文:

Plenty of ways you can achieve:

Solution 1: Implement the ternary conditional to display title in the view.

&lt;ul&gt;
    &lt;li *ngFor=&quot;let d of data&quot;&gt;
      {{ d.sex == &#39;Male&#39; ? &#39;Mr.&#39; : d.sex == &#39;Female&#39; ? &#39;Mrs.&#39; : &#39;&#39; }} {{ d.firstname }} {{d.lastname}}
    &lt;/li&gt;
&lt;/ul&gt;

Solution 2: Add the title properties in each object for the data array.

this.data = this.data.map((x) =&gt; ({
  ...x,
  title: x.sex == &#39;Male&#39; ? &#39;Mr.&#39; : x.sex == &#39;Female&#39; ? &#39;Mrs.&#39; : &#39;&#39;,
}));
&lt;ul&gt;
    &lt;li *ngFor=&quot;let d of data&quot;&gt;
      {{ d.title }} {{ d.firstname }} {{d.lastname}}
    &lt;/li&gt;
&lt;/ul&gt;

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 &#39;@angular/core&#39;;

@Pipe({
  name: &#39;title&#39;
})
export class TitlePipe implements PipeTransform {
  transform(value: any, args?: any): any {
    return value == &#39;Male&#39; ? &#39;Mr.&#39; : value == &#39;Female&#39; ? &#39;Mrs.&#39; : &#39;&#39;;
  }
}

Make sure that you have imported the pipe into the app module before using it.

&lt;ul&gt;
    &lt;li *ngFor=&quot;let d of data&quot;&gt;
      {{ d.sex | title }} {{ d.firstname }} {{d.lastname}}
    &lt;/li&gt;
&lt;/ul&gt;

Demo @ StackBlitz

答案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:

&lt;ul&gt;
    &lt;li *ngFor=&quot;let d of data&quot;&gt;
      {{ d.sex === &#39;Male&#39; ? &#39;Mr.&#39; : &#39;Mrs.&#39; }} {{ d.firstname }} {{ d.lastname }}
    &lt;/li&gt;
&lt;/ul&gt;

huangapple
  • 本文由 发表于 2023年7月27日 16:42:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定