Created
January 21, 2025 08:22
-
-
Save Armenvardanyan95/9e41f103b704a29df89b8d99b6856e60 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: ` | |
| <div> | |
| <label for="continent">Continent:</label> | |
| <select id="continent" [(ngModel)]="selectedContinent"> | |
| @for(continent of continents; track continent) { | |
| <option [value]="continent">{{ continent }}</option> | |
| } | |
| </select> | |
| </div> | |
| @if (selectedContinent()) { | |
| <div> | |
| <label for="country">Country:</label> | |
| <select id="country" [(ngModel)]="selectedCountry"> | |
| @for (country of countries(); track country) { | |
| <option [value]="country">{{ country }}</option> | |
| } | |
| </select> | |
| </div> | |
| } | |
| `, | |
| }) | |
| export class CascadingDropdownsComponent { | |
| continents = [ | |
| 'Africa', | |
| 'Asia', | |
| 'Europe', | |
| 'North America', | |
| 'Oceania', | |
| 'South America', | |
| ] as const; | |
| continentCountries = { | |
| Africa: ['Nigeria', 'Egypt', 'South Africa'], | |
| Asia: ['China', 'India', 'Japan'], | |
| Europe: ['Germany', 'France', 'United Kingdom'], | |
| 'North America': ['United States', 'Canada', 'Mexico'], | |
| Oceania: ['Australia', 'New Zealand', 'Fiji'], | |
| 'South America': ['Brazil', 'Argentina', 'Chile'], | |
| }; | |
| selectedContinent = signal<typeof this.continents | ''>(''); | |
| countries = computed( | |
| () => this.continentCountries[this.selectedContinent()] ?? [] | |
| ); | |
| selectedCountry = signal<string>(''); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment