Hi. I have the following issue in Nativescript Angular:
I call data from an API (using fetch) and display the data in a view. After I leave the page, if I then return to it, the original data is still there on the page, and the new data is called again, so the data is showing twice (or more if you do this more). The original data should not be there, but for some reason is sticking around in the view.
For example, say when I go to a certain page I load the following data on the page:
Red, Blue, Yellow
If I leave the page, but then later return to it, the original data is still there, and new data is also loaded, so the page will say:
'Red, Red, Blue, Blue, Yellow, Yellow'
and so on. This happens despite the fact that the data is supposed to be called fresh with each time the page loads.
Has anyone found a solution for this, so that the original data is removed from the page?
This may be an issue more so with Angular itself.
I have tried different methods of trying to clear the data array, to no avail.
Here is example code: each time the page loads, the data is called again:
ts:
public colors: Array<any> = [];
getAPI(){
var token = TOKEN
var url = URL
return fetch(url, {
method: "GET",
headers: new Headers({'Authorization': 'Bearer ' + TOKEN})
}).then((response) => {
return response.json();})
.then((returnedData) => {
this.returnedData.forEach((item)=>{
this.colors.push(item)
})
})
}
html:
<GridLayout>
<RadListView [items]="colors">
<ng-template tkListItemTemplate let-color="item" >
<StackLayout>
<Label [text]="color"></Label>
</StackLayout>
</ng-template>
</RadListView>
</GridLayout>