【发布时间】:2017-08-26 00:38:06
【问题描述】:
我是 Angular 新手,听起来可能很傻,但我不明白这段代码是如何工作的
this.ms.getList().then((hl) => { this.HeroesList = hl; });
我调用了服务方法,成功后返回什么?
其次,这个箭头函数如何获取我的服务必须返回的英雄数组,然后将数组分配给我的组件变量,
我的组件代码:
constructor(private ms: myService) {
}
ngOnInit() {
this.ms.getList().then((hl) => { this.HeroesList = hl; });
}
这里是服务:
import { Injectable } from '@angular/core'
import { Hero } from './hero'
import { HeroesList } from './heroesList'
@Injectable()
export class myService {
heroes: Hero[];
getList(): Promise<Hero[]> {
return Promise.resolve(HeroesList);//when success, return the list
}
}
【问题讨论】:
标签: angular promise angular-promise