我的代碼:刷新頁面後爲什麼我的項目被更改?
的.html
<ion-header>
<ion-navbar color="secondary">
<ion-title>Reviews</ion-title>
<ion-buttons end>
<button ion-button icon-only (click)="addReview()">
<ion-icon name="add"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content>
<ion-list no-lines>
<ion-item-sliding *ngFor="let review of reviews">
<ion-item>
<ion-avatar item-left>
<img src="https://api.adorable.io/avatars/75/{{review.title}}">
</ion-avatar>
<h2>{{review.title}}</h2>
<p>{{review.description}}</p>
<ion-icon *ngIf="review.rating < 50" danger name="sad"></ion-icon>
<ion-icon *ngIf="review.rating >= 50" secondary name="happy"></ion-icon>
{{review.rating}}
</ion-item>
</ion-item-sliding>
</ion-list>
</ion-content>
.TS
import { Component } from '@angular/core';
import { NavController, ModalController } from 'ionic-angular';
import { Reviews } from '../../providers/reviews';
import { AddReviewPage } from '../add-review-page/add-review-page';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
reviews: any;
constructor(public navCtrl: NavController, public reviewService: Reviews, public modalCtrl: ModalController) {
}
ionViewDidLoad() {
this.reviewService.getReviews().then((data) => {
console.log(data);
this.reviews = data;
});
}
addReview() {
let modal = this.modalCtrl.create(AddReviewPage);
modal.onDidDismiss(review => {
if (review) {
this.reviews.push(review);
// console.log('inside modal',review);
this.reviewService.createReviews(review);
}
});
modal.present();
}
}
.providers
import { Injectable } from '@angular/core';
import { Http,HttpModule } from '@angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the Reviews provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class Reviews {
data: any;
constructor(public http: Http) {
console.log('Hello Reviews Provider');
this.data = null;
}
getReviews() {
if (this.data) {
return Promise.resolve(this.data);
}
return new Promise(resolve => {
this.http.get('http://localhost:8080/api/reviews').map(res => res.json()).subscribe(data => {
this.data = data;
resolve(this.data);
});
});
}
createReviews(review){
this.http.post('http://localhost:8080/api/reviews',JSON.stringify(review)).subscribe(res=>{
console.log('inside create review', res.json());
})
}
}
當我添加數據,項目用不同的化身添加成功。但是,當我刷新頁面的項目被刪除和ava焦油得到改變,爲什麼呢?下面提供了 屏幕截圖。
看起來這是您的'../../providers/reviews';中的問題。 在該提供程序中,您通常將它們永久保存到API或localStorage。 你可以顯示該提供者的代碼嗎? –
@Hendrik Driesen,我已經更新了代碼。 – Aditya
'createReviews(review)'做了什麼? – echonax