Created
December 2, 2024 07:30
-
-
Save Armenvardanyan95/2c0df50f7670534a90ec21b9331320e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component({ | |
| template: ` | |
| <h1>Capitals</h1> | |
| <ul> | |
| @for (capital of capitals.value(); track capital) { | |
| <li>{{ capital }}</li> | |
| } | |
| </ul> | |
| `, | |
| }) | |
| export class CapitalsComponent { | |
| readonly #http = inject(HttpClient); | |
| capitals = rxResource({ | |
| loader: () => | |
| this.#http | |
| .get<Country[]>('https://restcountries.com/v3.1/all?fields=capital') | |
| .pipe( | |
| // map the response with RxJS | |
| map((res) => res.map((country) => country.capital[0])) | |
| ), | |
| }); | |
| } | |
| @Component({ | |
| template: ` | |
| <h1>Capitals</h1> | |
| <ul> | |
| @for (capital of capitals(); track capital) { | |
| <li>{{ capital }}</li> | |
| } | |
| </ul> | |
| `, | |
| }) | |
| export class CapitalsComponent { | |
| readonly #http = inject(HttpClient); | |
| countries = rxResource({ | |
| loader: () => | |
| this.#http.get<Country[]>( | |
| 'https://restcountries.com/v3.1/all?fields=capital' | |
| ), | |
| }); | |
| // use a separate computed property | |
| capitals = computed( | |
| () => this.countries.value()?.map((country) => country.capital[0]) ?? [], | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment