ton*_*9uk 2 angular2-template angular angular5
我想将我的枚举显示为字符串,但将其显示为数字。
我正在从Web服务接收json对象,并将其映射到我的Typescript对象
getProperties(): Observable<Property[]> {
return this.http.get<Property[]>(this.getallagentsproperties + '1');
}
export enum LetType {
Notspecified = 0,
LongTerm = 1,
ShortTerm = 2,
Commercial = 4
}
export class Property {
...other stuff...
letType: LetType;
...other stuff...
}
Run Code Online (Sandbox Code Playgroud)
我的组件会进行调用并将其添加到相关属性中
import { Component, OnInit } from '@angular/core';
import { Property } from './Property';
import { PropertyService } from '../properties.service';
@Component({
selector: 'app-properties',
templateUrl: './properties.component.html',
})
export class PropertiesComponent implements OnInit {
properties: Property[];
constructor(private propertyService: PropertyService) { }
ngOnInit() {
this.getProperties()
}
getProperties(): void {
this.propertyService.getProperties()
.subscribe(p => this.properties = p);
}
}
Run Code Online (Sandbox Code Playgroud)
当我{{property.letType}}在模板中显示时,将显示:
4 我希望它显示广告
我试图遵循在我添加的模板中此处找到的答案
{{LetType[property.letType]}}
Run Code Online (Sandbox Code Playgroud)
在我的Componant中,我添加了
LetType = this.LetType;
Run Code Online (Sandbox Code Playgroud)
但我总是在控制台中收到以下错误
无法读取未定义的属性“ 4”
我究竟做错了什么?
感谢@Harry Ninh 的评论,我能够解决我的问题。
{{property.letType}}显示0我想要它显示My Enum Value
注意:我没有在我的问题中要求拆分大小写,但我在这里提供了这一点,因为我觉得许多将枚举显示为字符串值的人会发现它很有用
步骤 1 将所需的枚举(在我的情况下为 LetType)添加到您的组件
import { Component, OnInit, Type } from '@angular/core';
import { Property, LetType} from './Property';
import { PropertyService } from '../properties.service';
@Component({
selector: 'app-properties',
templateUrl: './properties.component.html',
})
export class PropertiesComponent implements OnInit {
properties: Property[];
LetType = LetType;
constructor(private propertyService: PropertyService) { }
ngOnInit() {
this.getProperties();
}
getProperties(): void {
this.propertyService.getProperties()
.subscribe(p => this.properties = p);
}
}
Run Code Online (Sandbox Code Playgroud)
第 2 步创建自定义管道以将您的数字转换为字符串
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'eNumAsString'
})
export class ENumAsStringPipe implements PipeTransform {
transform(value: number, enumType: any): any {
return enumType[value].split(/(?=[A-Z])/).join().replace(",", " ");;
}
}
Run Code Online (Sandbox Code Playgroud)
第 3 步将您的管道添加到表格中
...other html table tags...
<td>{{property.letType | eNumAsString:LetType}}</td>
...other html table tags...
Run Code Online (Sandbox Code Playgroud)
您无需在此处创建管道。这是我的答案。
let-type.enum.ts
export enum LetType{
Type1 = 1,
Type2 = 2,
Type3 = 3,
Type4 = 4
}
Run Code Online (Sandbox Code Playgroud)
property.model.ts
export interface Property{
...other stuff...
letType: LetType;
...other stuff...
}
Run Code Online (Sandbox Code Playgroud)
properties.component.ts
import { Component, OnInit } from '@angular/core';
import { Property} from './property.model';
import { LetType} from './let-type.enum';
import { PropertyService } from '../properties.service';
@Component({
selector: 'app-properties',
templateUrl: './properties.component.html',
})
export class PropertiesComponent implements OnInit {
properties: Property[] = [];
LetType = LetType;
constructor(private propertyService: PropertyService) { }
ngOnInit() {
this.getProperties();
}
getProperties() {
this.propertyService.getProperties().subscribe(result => {
this.properties = result
});
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的HTML
<ng-container matColumnDef="columnName">
<th mat-header-cell *matHeaderCellDef >Types</th>
<td mat-cell *matCellDef="let element">{{LetType[element.letType]}}</td>
</ng-container>
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以在没有管道的情况下执行此操作:
LetType : typeof
LetType = LetType ;
Run Code Online (Sandbox Code Playgroud)
并在 html 页面中:
<td mat-cell *matCellDef="let element">{{LetType[element.letType]}}</td>
Run Code Online (Sandbox Code Playgroud)
像以前一样。
| 归档时间: |
|
| 查看次数: |
10097 次 |
| 最近记录: |