Angular 5将Enum int转换为字符串

时间:2018-01-12 00:35:54

标签: angular angular2-template 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...
}

我的组件拨打电话并将其添加到相关属性

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);
  }
}

当我在模板中显示{{property.letType}}时显示:

4我希望它显示商业

我尝试按照我添加的模板中找到的答案here

{{LetType[property.letType]}}

在我的Componant中我添加了

LetType = this.LetType;

但我总是在控制台中得到以下错误

  

无法读取未定义的属性'4'

我做错了什么?

3 个答案:

答案 0 :(得分:5)

您无需在此处创建管道。这是我的答案。

let-type.enum.ts

export enum LetType{
  Type1 = 1,
  Type2 = 2,
  Type3 = 3,
  Type4 = 4
}

property.model.ts

export interface Property{
   ...other stuff...
   letType: LetType;
   ...other stuff...
}

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
    });
  }
}

然后在您的html中

<ng-container matColumnDef="columnName">
   <th mat-header-cell *matHeaderCellDef >Types</th>
   <td  mat-cell *matCellDef="let element">{{LetType[element.letType]}}</td>
</ng-container>

答案 1 :(得分:2)

您可以在不使用管道的情况下执行此操作:

LetType : typeof 
LetType = LetType ;

,并在html页面中:

<td  mat-cell *matCellDef="let element">{{LetType[element.letType]}}</td>

和以前一样。

答案 2 :(得分:0)

感谢@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);
    }
}

步骤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(",", " ");;
    }
}

步骤3将管道添加到表格

...other html table tags...
    <td>{{property.letType | eNumAsString:LetType}}</td>
...other html table tags...
相关问题