I'm pretty newbie in angular and I hope someone can help me >.<
I have a component which has an object array as property. The thing is that when I update that array with a new object all the items in array became the last object that I have pushed
this is the component im trying to update:
export class DisplayComponent {
weather: Weather = {
city: null,
conditions: null,
temperature: null,
icon: null,
humidity: null
}
history: Array<Weather> = []
update(weather: Weather) {
this.weather = weather
console.log(weather)
this.history.push(weather)
console.log(this.history)
}
}
This is what console logs returns after trying to insert the first object:
Weather {city: "Granada", conditions: "Clouds", temperature: 25.38, icon: "http://openweathermap.org/img/wn/02d@2x.png", humidity: 83}
0: Weather {city: "Granada", conditions: "Clouds", temperature: 25.38, icon: "http://openweathermap.org/img/wn/02d@2x.png", humidity: 83}
But when i try to add another one I get this:
Weather {city: "Barcelona", conditions: "Clouds", temperature: 13.89, icon: "http://openweathermap.org/img/wn/02d@2x.png", humidity: 54}
0: Weather {city: "Barcelona", conditions: "Clouds", temperature: 13.89, icon: "http://openweathermap.org/img/wn/02d@2x.png", humidity: 54}
1: Weather {city: "Barcelona", conditions: "Clouds", temperature: 13.89, icon: "http://openweathermap.org/img/wn/02d@2x.png", humidity: 54}
As you can see what i have now is "barcelona" two times when granada was the first one.
Edit:
This is the function that submit the object:
submit() {
this.weatherData.load(this.city).subscribe(data => {
this.weather.city = data['name']
this.weather.conditions = data['weather'][0]['main']
this.weather.temperature = data['main']['temp']
this.weather.icon = this.weatherData.getIconUrl(data['weather'][0]['icon'])
this.weather.humidity = data['main']['humidity']
this.onSelection.emit(this.weather)
})
Coud someone help me with this? thanks in advance!!