【问题标题】:Angular 2 Observables: Secure way to get the value that return my serviceAngular 2 Observables:获取返回服务的价值的安全方法
【发布时间】: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


    【解决方案1】:

    您可以创建服务来存储数据和获取数据 创建一个变量来存储数据 创建方法来设置数据和获取数据

    //数据服务

    import { Injectable } from '@angular/core';
    @Injectable()
    export class CommonDataService {
     public Data:any;
    
        getData(): any{
                return this.Data;
            }
    
            setData(data: any) {
                this.Data= data;
            }
    }
    

    当你想存储数据时调用服务的set方法,当你想获取数据时调用服务的get方法

    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']);
             this._commonDataService.setData(JSON.stringify(lu));
             }, error => {
                this.ShowMsg(error);
          });
          this.templateFields[key].Lookups =   JSON.parse(this._commonDataService.getData()));
        }
        //localStorage.removeItem(this.templateFields[key].Field);
      }
    }
    

    【讨论】:

    • 感谢您的回答,但是,那个可能的解决方案对我不起作用,现在我正在寻找另一种方式,我再次思考这种调用查找的方式是否正确。跨度>
    猜你喜欢
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多