Reputation: 347
hi I'm new to angular
and typescript
, and I'm trying to learn the tutorial from angular. and I found something that I don't understand what it meant. like:
1.
getHeroes(): Observable<Hero[]> {
this.messageService.add('HeroService: fetched heroes');
console.log(this.http);
return this.http.get<Hero[]>(this.heroesUrl);
}
on the above example, i want to know what does <Hero[]>
means on Observable<Hero[]>
and this.http.get<Hero[]>(this.heroesUrl)
2.
getHero(id: number): Observable<Hero> {
this.messageService.add(`HeroService: fetched hero id=${id}`);
return of(HEROES.find(hero => hero.id === id));
}
check it carefully on HeroService: fetched hero id=${id}
it wrapped with backticks (`) not single quote ('). why use it?
and last one the HEROES.find(hero => hero.id === id)
what does =>
means?
I'm really new to this. so, I'm so sorry if my questions offend or trigger you guys..
Thank you!
Upvotes: 2
Views: 1633
Reputation: 131
Method getHeroes() returns an Observable of type array's of Hero. And Backtick (`) is called template literal. Earlier we used to write
let a = 10
b = 5 ;
console.log("value of a is" + a + " and the value of b is " + b);
With template literals we can write
console.log(`value of a is ${a} and value of b is ${b}`)
Where ${expression} can be used to bind expression .
=> is called fat arrow .
let example = function(str){
console.log(str)
}
Can now be written using fat arrow in this way
let example = (str) =>{
console.log(str)
}
Please Go through the docs of ES6 for better understanding . Template literals and fat arrow are implemented in ES6 .
Upvotes: 2
Reputation: 222582
Observable<Hero[]>
Denotes that getHeroes method rObservable returns an Observable which wraps the data of type Hero Hero
, where []
denotes an array;
` (backtick) is known as template literal in javascript
An expression inside the literal (the value of which is evaluated during runtime and included in the final value produced by the literal) is enclosed in curly braces {} with a preceding dollar sign $.
Upvotes: 1