I want to be able to change the color between green and red when an ion-item is clicked.
The code below retrieves multiple names and colors from a JSON string and it works.
<ion-item *ngFor="let data of data" [style.backgroundColor]="data?.color" (click)="PostData(data)">
{{data?.name}}
</ion-item>
But the problem starts when I want to pass the data back to a function so I can use the PUT method to send the new data back to the API I got the JSON string from.
PostData(data) {
var url = 'http://www.external.tld/page/'+data?.id; // append the id to the url
if (data?.color == 'red') {data?.color = 'green'};
if (data?.color == 'green') {data?.color = 'red'}; // toggle color
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded'});
let options = new RequestOptions({ headers: headers });
return this.http.put(url, JSON.stringify({"name": new String(data?.name), "color": new String(data?.color)}), options)
.map(res => res.json()); // send name and toggled color
}
I am a noob with angular 2 so I have no idea what I am doing right and wrong.
Bonus: I also didn't get the PUT method to work yet (I even prefer to send the data (name and color) without JSON if possible).
data?.idisn't valid Typescript.