Hello there, I’m creating a news reader app, right now the new is coming from an API, the way I’m getting and showing is:
private url: string = "https://api.myjson.com/bins/1awtvn";
private itensSalvos: string = '';
this.remove = '';
constructor(public navCtrl: NavController, private alertCtrl: AlertController, public loadingCtrl: LoadingController, public http: Http, public actionSheetCtrl: ActionSheetController, private sharingVar: SocialSharing, public storage: Storage) {
this.remove = '';
this.fetchContent();
this.storage.get('saved_posts').then(itens => this.itensSalvos = itens);
if (this.itensSalvos == ""){
this.itensSalvos = "";
} else {
alert (this.itensSalvos);
}
}
fetchContent ():void {
let loading = this.loadingCtrl.create({
content: 'Buscando conteúdo...'
});
loading.present();
this.http.get(this.url).map(res => res.json())
.subscribe(data => {
this.feeds = data.data;
this.noFilter = this.feeds;
loading.dismiss();
});
}
And showing on the screen with:
<ion-card *ngFor="let feed of feeds">
<img (click)="itemSelected(feed)" [src]="feed.img" />
<ion-card-content (click)="itemSelected(feed)">
<ion-card-title>
{{feed.post_title}}
</ion-card-title>
<p [innerHTML]="feed.post_excerpt" id="justify"></p><br/>
<span [innerHTML]="feed.post_date" class="data_e_hora"></span> - <span>{{feed.autor}}</span>
</ion-card-content>
</ion-card>
What I’m trying to do is save a string with the post id to search later, for example 1, 12, 25…
To do that I created a button:
<ion-row no-padding>
<ion-col>
<button ion-button clear small color="primary" (click)="saveItem(feed.ID)" icon-left>
<ion-icon name='bookmark'></ion-icon>
</button>
</ion-col>
</ion-row>
inside the card and to handle this saveItem:
saveItem (post) {
this.storage.get('saved_posts').then(itens => this.itensSalvos = itens);
// alert ("Recebeu: " + post + "Está incluso em?" + this.itensSalvos);
if (this.itensSalvos.includes(post)){
let alert = this.alertCtrl.create({
title: 'Removed',
buttons: ['OK']
});
this.remove = post + ",";
this.itensSalvos = this.itensSalvos.replace (this.remove, "");
this.storage.set ('saved_posts', this.itensSalvos);
alert.present();
} else {
let alert = this.alertCtrl.create({
title: 'Saved!',
buttons: ['OK']
});
this.itensSalvos = this.itensSalvos + post;
this.itensSalvos = this.itensSalvos + (",");
this.storage.set ('saved_posts', this.itensSalvos);
alert.present();
}
}
The problem is that, every time I click on save I get two times removed and then two times in a row saved. I discovered that if I click save and reload the page every time I save or remove this ID it works perfectly but that’s a problem because for each saved post I’m going to have to reload the app.
How can I save the ID in a string without having to reload the page?
Thank you!