2016-07-26 81 views
0

我目前正在学习Angular2和typescript,并且这部分的angular 2文档导致了一个问题。有人可以在下面的例子中用它来解释=>吗?我在网上搜索并找到了对lambda函数和返回类型的引用,但是我找不到与=>的使用相匹配的任何内容,就像这里使用的那样。这是什么意思从angular2文档?

export class DashboardComponent implements OnInit { 
heroes: Hero[] = []; 
constructor(private heroService: HeroService) { } 
ngOnInit() { 
    this.heroService.getHeroes() 
    .then(heroes => this.heroes = heroes.slice(1, 5)); 
} 
gotoDetail() { /* not implemented yet */} 
} 

如果有人能帮助我理解这一点,我会非常感激。

+0

试试这个:https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html –

回答

3

下面

this.heroService.getHeroes() 
.then(heroes => this.heroes = heroes.slice(1, 5)); 

等价的:

var that = this; 
this.heroService.getHeroes() 
.then(function (heroes) { 
    return that.heroes = heroes.slice(1, 5)); 
}); 
1

这就是所谓的箭头的功能,并能在打字稿教程理解。

在你的代码中,'getHeroes'函数会得到一些响应,并且该响应将被存储在局部变量'heroes'中。

'英雄=>'=功能(英雄:任何) 其余部分可视为功能体。这是一个很好的做法,以保持参考活性和某些变量的局部性。