<div class="weather-app-wrapper" *ngIf="weather !== undefined">
<div class="weather-wrapper" *ngFor="let forecast of weather.forecastDays">
<div class="weather-card">
<div class="forecast-icon" [ngClass]="{{forecast.condition}}"></div>
<h1>{{forecast.tempMin}}º - {{forecast.tempMax}}º</h1>
<p>{{forecast.day}}</p>
</div>
</div>
</div>
I am attempting to assign a class to an HTML element in this line:
<div class="forecast-icon" [ngClass]="{{forecast.condition}}"></div>
however, I am encountering an error. Is there a way to achieve this in HTML or does it need to be done in TypeScript? I am familiar with applying classes in static HTML, but I am unsure how to do so within a loop that generates HTML elements. The property forecast.condition
contains a valid string, and the loop iterates 5 times without issue. Can you suggest a method to accomplish this?
For instance:
//forecast.condition = "sunny"
<div class="forecast-icon">
should transform into
<div class="forecast-icon sunny">
on the rendered page
Below is the Forecast model:
export class ForecastDay {
tempMin: number;
tempMax: number;
day: string;
condition: string;
}
This is the encountered error:
Uncaught Error: Template parse errors: Parser Error: Got interpolation ({{}}) where expression was expected at column 0 in [{{forecast.condition}}] in ng:///AppModule/WeatherComponent.html@3:39 (" weather.forecastDays">
<div class="weather-card">
<div class="forecast-icon" [ERROR ->][ngClass]="{{forecast.condition}}"></div>
<h1>{{forecast.tempMin}}º - {{forecast.tempMax"): ng:///AppModule/WeatherComponent.html@3:39 Parser Error: Unexpected token {, expected identifier, keyword, or string at column 2 in [{{forecast.condition}}] in ng:///AppModule/WeatherComponent.html@3:39 (" weather.forecastDays">
<div class="weather-card">
<div class="forecast-icon" [ERROR ->][ngClass]="{{forecast.condition}}"></div>
<h1>{{forecast.tempMin}}º - {{forecast.tempMax"): ng:///AppModule/WeatherComponent.html@3:39 Parser Error: Missing expected : at column 21 in [{{forecast.condition}}] in ng:///AppModule/WeatherComponent.html@3:39 (" weather.forecastDays">
<div class="weather-card">
<div class="forecast-icon" [ERROR ->][ngClass]="{{forecast.condition}}"></div>
<h1>{{forecast.tempMin}}º - {{forecast.tempMax"): ng:///AppModule/WeatherComponent.html@3:39 Parser Error: Unexpected token } at column 21 in [{{forecast.condition}}] in ng:///AppModule/WeatherComponent.html@3:39 (" weather.forecastDays">
<div class="weather-card">
<div class="forecast-icon" [ERROR ->][ngClass]="{{forecast.condition}}"></div>
<h1>{{forecast.tempMin}}º - {{forecast.tempMax"): ng:///AppModule/WeatherComponent.html@3:39 Parser Error: Unexpected token '}' at column 22 in [{{forecast.condition}}] in ng:///AppModule/WeatherComponent.html@3:39 (" weather.forecastDays">
<div class="weather-card">
<div class="forecast-icon" [ERROR ->][ngClass]="{{forecast.condition}}"></div>