我有一个脚本可以检索一些数据并将其存储在一个数组中:
export class ProductsByArea {
public products: any;
public area: any;
constructor(public navCtrl: NavController,
public navParams: NavParams,
private easyStockService: EasyStockService,
private imageProvider: ProductImageProvider,
private platform: Platform
)
{
//this.area = navParams.get('area');
this.easyStockService.getArea(navParams.get('area')).then((doc) => {
this.area = doc;
this.getProducts(this.area._id).then((products) => {
let datas: any;
datas = products;
this.products = datas;
});
});
}
private getProducts(area: any){
let datas: any = [];
let products: Array<any> = [];
return new Promise((resolve) => {
this.easyStockService.getProducts(area).then((results) => {
datas = results;
let uploadDirectory = this.imageProvider.getUploadDirectory();
// Boucle sur les produits pour récupérer les données cohérentes
datas.forEach((data) => {
let product: any = {};
product.ean = data._id;
product.title = data.title;
if(data.image != 'no-image.png')
product.image = uploadDirectory + data._id + '.jpg';
else
product.image = 'assets/images/no-image.png';
product.stock = this._computeStock(data.inStock);
products.push(product);
})
resolve(products);
}, (error) => {
console.log('Erreur lors de la récupération des produits dans la zone : ' + area);
});
});
和...
private _computeStock(inStock:any){
let averagePrice: number = 0;
let totalStock: number = 0;
let lastPurchase: any = null;
let lastOutDate: any = null;
let totalValue: number = 0;
inStock.forEach((stock) => {
averagePrice += parseFloat(stock.totalPrice) / parseInt(stock.initialQuantity);
totalStock += parseInt(stock.initialQuantity);
lastPurchase = moment(stock.purchaseDate).format('DD-MM-YYYY');
let out = stock.out;
if(out.length){
totalStock -= out.quantity;
}
});
totalValue = totalStock * (averagePrice / inStock.length);
return {'totalStock': totalStock, 'lastPurchaseDate': lastPurchase, 'lastOutDate': lastOutDate, 'totalValue': totalValue};
}
正确检索数据,但是当我想在视图中显示它们时,我得到了这种屏幕:
视图代码为:
<ion-content>
<ion-list inset id="products-by-area">
<ion-item-sliding *ngFor="let product of products">
<ion-item class="item-thumbnail-left item-icon-right item-complex">
<ion-thumbnail item-left>
<img src="{{ product.image }}" title="{{ product.title }}" />
</ion-thumbnail>
<h2>{{ product.title }}</h2>
<ion-note item-end>
{{ product.ean }}
</ion-note>
</ion-item>
<ion-item-options side="right" *ngIf="product.stock.totalStock > 0">
<button ion-button color="warning">
<ion-icon name="remove-circle"></ion-icon>
{{ 'remove' | translate }}
</button>
<button ion-button color="danger" (click)="toTrash(product)";>
<ion-icon name="trash"></ion-icon>
{{ 'trash' | translate }}
</button>
<button ion-button color="success">
<ion-icon name="add-circle"></ion-icon>
{{ 'add' | translate }}
</button>
</ion-item-options>
<ion-item-options side="left">
<button ion-button color="success">
<ion-icon name="stats"></ion-icon>
{{ 'stats' | translate }}
</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
</ion-content>
不明白为什么不显示product.title或product.image,甚至product.ean?
尝试修改公共产品属性的类型,但是......没有任何内容,就像产品变量不是对象或空对象一样。
我是离子2的新手,并且真的不明白它的错误。