【发布时间】:2018-03-31 18:16:25
【问题描述】:
我正在寻找方法来处理这个 observable 向我抛出的结果,并且我总是可以从回调外部获取服务返回给我的值。
我提出的一个问题:
经常会出现这种情况,虽然服务成功返回值给我,但我无法成功将信息保存到this.templateFields[key].Lookup
private getLookUpsFromFields() {
var lu: any[] = [];
//console.log('templatefields', this.templateFields);
for (const key in this.templateFields) {
if (this.templateFields[key].SimpleDataType === 'String List, Multi' ||
this.templateFields[key].SimpleDataType === 'String List, Single') {
//console.log('Field Lookup', this.templateFields[key].Field);
this._lookUpsService.getLookUpsOfField(this.model, this.templateFields[key].Field).subscribe(success => {
lu = this._lookUpsService.getArrayLook(success['metadataAllLookUpField']);
localStorage.setItem(this.templateFields[key].Field, JSON.stringify(lu));
}, error => {
this.ShowMsg(error);
});
this.templateFields[key].Lookups = JSON.parse(localStorage.getItem(this.templateFields[key].Field));
}
//localStorage.removeItem(this.templateFields[key].Field);
}
}
//查找服务
getLookUpsOfField(model: string, field: string){
//console.log(this.url + '/' + model + '/' + propertyType);
return this._httpS.get(this.url + '/' + model + '/' + field);
}
getArrayLook(lookups: any) {
var array: any[] = [];
if (lookups !== undefined && lookups !== null) {
lookups.map(x => {
array.push({Field: x.lookup_values.split(x.delimiter)[0], value: x.lookup_values.split(x.delimiter)[1]});
});
}
return array;
}
我知道处理服务返回值的一种方法是在 observable 范围内处理它们,但我还有其他选择吗?除了我展示的这两种选择之外: 1-) 使用本地存储 2-) 在可观察范围内处理逻辑
我要保证this.templateFields[key].Lookup总是收到回调的返回值。
其他选择? 将提供任何额外信息。 我正在使用角度 2+
【问题讨论】:
标签: angular