【问题标题】:Angular 5 convert Enum int to stringAngular 5将枚举int转换为字符串
【发布时间】:2018-06-21 10:12:18
【问题描述】:

我想将我的枚举显示为字符串,但它显示为数字。

我正在从 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]}}

在我的组件中我添加了

LetType = this.LetType;

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

无法读取未定义的属性“4”

我做错了什么?

【问题讨论】:

标签: angular angular2-template angular5


【解决方案1】:

你可以在没有管道的情况下做到这一点:

LetType : typeof 
LetType = LetType ;

在html页面中:

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

和以前一样。

【讨论】:

    【解决方案2】:

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

    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>
    

    【讨论】:

      【解决方案3】:

      感谢@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...
      

      【讨论】:

      • 如果我不应用管道,我会得到一个空的组合框
      猜你喜欢
      • 2017-07-07
      • 2010-10-03
      • 1970-01-01
      • 2014-03-18
      • 2015-06-10
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      相关资源
      最近更新 更多