Skip to content

Instantly share code, notes, and snippets.

@Armenvardanyan95
Created December 2, 2024 07:30
Show Gist options
  • Save Armenvardanyan95/2c0df50f7670534a90ec21b9331320e3 to your computer and use it in GitHub Desktop.
Save Armenvardanyan95/2c0df50f7670534a90ec21b9331320e3 to your computer and use it in GitHub Desktop.
@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