November 2019
Beginner
804 pages
20h 1m
English
The displayCountries method receives an array of Country objects and renders those as <option> HTML elements as follows:
displayCountries(countries: Country[]): void {
if (!countries) {
throw new Error("The list of countries to display must be provided!");
} else if (countries.length === 0) {
throw new Error("The list of countries cannot be empty!");
}
console.log("Displaying the countries");
let countriesOptions = "";
countries.forEach(country => {
countriesOptions += `<option value="${country.id}">${country.name}</option>`;
});
this._countrySelect.innerHTML = countriesOptions;
}
Nothing special to mention here; this is a technique that we've already used before.
Read now
Unlock full access