Angular 2 promise不会返回任何值

时间:2017-09-28 17:07:01

标签: javascript angular devextreme

我想在promise返回一个值后才渲染我的数据小部件。但我遇到了一个问题,我目前无法从我的承诺中看到任何返回值,我的数据小部件也没有得到渲染。我正在我的构造函数中进行此登录。

我的plunker链接如下所示,以便您查看代码 https://plnkr.co/edit/kxMqwFZ1T5tU4xRcqomW?p=preview

 constructor(myService: SummaryService) {
    this.selectedYear = (new Date()).getFullYear() + ""; 
    //Get years
    myService.getSummaryYears().then(
      response  => {
        this.summaryYears = response;
        console.log(this.summaryYears);
        this.createPivotGrid(myService);
      }
    ).catch(error => {
      console.log("Unable to get years, error => " + error);
    })        
    }

服务电话

@Injectable()
export class SummaryService {
  getSummaries():Promise<Response> {
    return new Promise(resolve =>
      setTimeout(() => resolve(summaries), 5000)
    );
  }

    getSummaryYears(): Promise<Response> {
    return new Promise(resolve =>
      setTimeout(() => resolve(summaryYears), 5000)
    );
  }
}

1 个答案:

答案 0 :(得分:0)

有OnInit回调用于初始化小部件的数据 这是固定版本:

ngOnInit() {
     this.myService.getSummaryYears().then(
      response  => {
        console.log('summary years response %O', response)
        this.summaryYears = response;
        console.log(this.summaryYears);
        this.createPivotGrid();
      }
    ).catch(error => {
      console.log("Unable to get years, error => " + error);
    })
  }

https://plnkr.co/edit/wP7jLwZCpUwJhPqEPNbD?p=preview