【发布时间】:2019-05-07 21:05:13
【问题描述】:
我正在制作 Angular 应用程序,我正在其中进行服务调用,
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 中之后,我需要调用这个questions.push(new DropdownQuestion(element))
我需要在 forEach 函数中进行此调用,所以如果我使用 await,它会给出错误 IDE,
[ts] 'await' expression is only allowed within an async function...
【问题讨论】:
标签: angular typescript angular5 angular6 angular-services