【发布时间】:2016-06-22 07:52:31
【问题描述】:
我正在做一个项目,我想将后端枚举的 id 转换为字符串形式,以便前端更容易理解。
为此,我想使用管道。 管道应与 API 联系,在该 API 中获取带有键和值的 JSON 对象。
我的问题是我似乎无法让转换函数等到我收到 api 数据并将其存储在变量中。
我知道如何联系 api 并获取对象。我已经做了一个转换,可以做我需要做的事情。我只是想不通如何让两人一起交谈。
这是我现在拥有的:
import {Pipe, PipeTransform} from 'angular2/core';
import {ApiHelper} from '../../services/api.service';
@Pipe({ name: 'userType' })
export class userType implements PipeTransform {
private typeObject;
constructor(private ApiService: ApiHelper) {
ApiService.getAllTypes().subscribe(
types => this.storeTypes(types)
);
}
storeTypes(types){
this.typeObject = types;
}
transform(value: number, args: string[]): any {
var userType;
for(var type in this.typeObject){
if(type.value == value){
usertype = type.key;
}
}
return userType;
}
}
希望有人可以帮助或为我指明正确的解决方案。
____ 编辑:_____
作为一个新手,这是我从 Günter Zöchbauer 的回答中了解到的。 这对我的看法没有任何回报。
import {Pipe, PipeTransform} from 'angular2/core';
import {ApiHelper} from '../../services/api.service';
@Pipe({ name: 'userType' })
export class userType implements PipeTransform {
constructor(ApiService: ApiHelper)
transform(value: number, args: string[]): any {
return new Promise((resolve, reject) => {
this.ApiService.getAllTypes().subscribe(typeObject => {
var userType;
for (var type in typeObject) {
if (type.value == value) {
usertype = type.key;
}
}
resolve(userType);
});
});
}
}
【问题讨论】:
标签: api pipe angular transform