I'm testing out writing a simple application in Angular2 to display the weather of a city. I've created a service to retrieve JSON data from OpenWeatherMap when a user types in a city and submits a form. Inside my service, I have:
getDummyWeather() {
return {"weather":{"main":"nice and sunny!"}};
}
I have the data stored as a JSON object called weather in my component. I figured that the call inside my template to get main's string would be {{weather.weather.main}} but then I get errors stating that the value main cannot be retrieved from undefined. Obviously, it's undefined until I submit the form. When I submit the form, nothing happens. So then I tried {{weather.main}} and I do not receive any errors, but I don't get any data back either. If I stringify the JSON, I get the full JSON string to display on form submit. Using {{weather}} displays [Object object] on form submit.
How do I get data from a JSON object to display in my template in Angular2?
{"weather":"Nice and sunny"}by using{{weather.weather}}. I cannot get JSON from the sample JSON in my question above because of the nesting.{{variable | json}}is the answer