简体   繁体   中英

What does this mean from the Angular documentation?

I am currently learning Angular and typescript and this part of the Angular documentation is causing a problem. Could someone give an explanation of => in the context of how it is used in the example below? I searched online and found references to lambda functions and return types but I couldn't find anything that matched the use of => like it is used here.

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 */}
}

If anyone can help me understand this I would really appreciate it.

Below

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

is equivalent of:

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

This is called arrow function and this can be understood in TypeScript tutorials.

In your code 'getHeroes' function will get some response back and that response will be stored in local variable 'heroes'.

'heroes=>' = function(heroes:any) and rest of the part can be considered as function body. This is a good practice to keep the reference alive and local of some variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM