如何从toPromise()中以角度获取值?

时间:2018-12-06 07:49:33

标签: angular typescript angular5 angular6 angular-services

我正在进行类似应用程序的角向应用程序

let newValue =  this.dynamicService.getRest(element.optionsUrl,localStorage.getItem('token')).toPromise();

console.log(newValue)给出

{"__zone_symbol__state":null,"__zone_symbol__value":[]}

在这里,我需要将服务中的值存储到变量newValue中。

如果使用toPromise.then.toPromise().then(function (result) { }),我也会得到相同的结果。

请帮助我将toPromise()的服务值存储到变量newValue。.

编辑:

构造函数:

  constructor(private http: HttpClient, private dynamicService: NgiDynamicFormsService) {
    this.newArray = AppConfig.settings.template.templateFormJSON;
  }

异步getQuestions()

          async  getQuestions() {

            let questions: any = [];

            this.newArray.forEach(element => {
          if (element.elementType === 'textbox') {
            questions.push(new TextboxQuestion(element));
          } else if (element.elementType === 'dropdown') {
                let newValue =  await this.dynamicService.getRest(element.optionsUrl,localStorage.getItem('token')).toPromise();
    element.options = newValue;
    questions.push(new DropdownQuestion(element));
            } else if (element.elementType === 'textarea') {
            questions.push(new TextareaQuestion(element));
          } else if (element.elementType === 'checkbox') {
            questions.push(new CheckboxQuestion(element));
          }
            });
return questions.sort((a, b) => a.order - b.order);
  }

在这里您可以看到,在获取newValue之后,我需要将newValue中的值发送到element.options。然后,我需要调用questions.push(new DropdownQuestion(element));为此,我无法获取newValue中的值,因此questions.push(new DropdownQuestion(element))给出了空值,因此在将值存储在newValue中之后,我需要调用此值{{ 1}}

我需要在questions.push(new DropdownQuestion(element))函数中进行此调用,因此,如果我使用 await ,它将给出错误IDE,即

forEach

2 个答案:

答案 0 :(得分:1)

要读取承诺值,请使用可链接的.then运算符。

let newValue =  this.dynamicService.getRest(element.optionsUrl,localStorage.getItem('token')).toPromise();

newValue.then((value)=>console.log(value));

您还可以使用aynsc / await

async func(){
   let newValue =  await this.dynamicService.getRest(element.optionsUrl,localStorage.getItem('token')).toPromise();
}

---- Promise.all ----

async  getQuestions() {

    let questions: any = [];
    let questionsPromise: any = [];
    let questionsPromiseResult: any = [];
    this.newArray.forEach(element => {
       if (element.elementType === 'textbox') {
          questions.push(new TextboxQuestion(element));
       } else if (element.elementType === 'dropdown') {
        questionsPromise.push( this.dynamicService.getRest(element.optionsUrl,localStorage.getItem('token')).toPromise());
        questionsPromiseResult.push(element);
      } else if (element.elementType === 'textarea') {
        questions.push(new TextareaQuestion(element));
      } else if (element.elementType === 'checkbox') {
        questions.push(new CheckboxQuestion(element));
      }
    });

    Promise.all(questionsPromise).then(results =>{
       results.forEach(item,index=>{
          let element = this.questionsPromiseResult[index];
          element.options = item;
          questions.push(new DropdownQuestion(element));
       });
    });

答案 1 :(得分:1)

我提供了一个示例,希望它可以使您了解所需的内容:

在您的服务文件中:

getTransactions(): Promise<{any}> {
  return this.http.get(`${Api.Url}transaction`).toPromise()
    .then(r => {
      return r;
    }).catch(error => {
      return Promise.reject(error);
    });
}

以及要使用该服务并获取数据的component.ts中:

this.transactionService.getTransactions().then(
        r => {
            console.log(r);
        }
    ).catch( e => {
        alert('error fetching data');
    });