[英]How pass array with objects from provider to page?
I have a map structure in provider and I have to show values from map objects in my template. 我在提供程序中有一个地图结构,我必须在模板中显示地图对象的值。 My provider store.ts:
我的提供者store.ts:
import { HttpClient } from '@angular/common/http';
import { HttpClientModule } from '@angular/common/http'
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { JsonStoresDataInterface } from './../../providers/stores/stores';
/*
Generated class for the StoresProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class StoresProvider {
private JsonStoresData = new Map();
url_request = "path/to/json";
constructor(public http: HttpClient) {
}
getRequest(callback){
this.JsonStoresData.clear();
this.http.get<JsonStoresDataInterface>(this.url_request).subscribe(data => {
var json = data.data;
for (var store of json.stores){
if(store.polygon_id[0] != null){
var obj = { name: store.name,
description: store.description,
floor: store.floor,
image: store.pic_info.src,
uid: store.uid }
this.JsonStoresData.set(store.polygon_id, obj)
}}
callback(this.JsonStoresData);
});
}
getStoresRemote(){
return new Promise(resolve => {
//let def_key = this.defaultKey;
this.getRequest(function(data){
let storeArray = [];
storeArray = Array.from(data.values());
storeArray.sort((a,b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0));
let storeName = storeArray.map(a => a.name);
resolve(storeArray);
});
});
}
}
home.ts: home.ts:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { MenuComponent } from "../../components/menu/menu";
import { StoresProvider } from './../../providers/stores/stores';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public buttonClicked: boolean = false;
public btnActive: string = "";
img;
description;
storeArray;
openStore(i) {
document.getElementById('main').className += ' no-scroll';
document.getElementById('card').className += ' active';
this.buttonClicked = !this.buttonClicked;
this.description = this.storeArray[i].description;
console.log(this.description);// Cannot read property 'description' of undefined
at
}
ionViewDidLoad() {
this.storeArray = this.storesProvider.getStoresRemote();
console.log('this.storeArray');
}
constructor(public navCtrl: NavController, public storesProvider: StoresProvider) {
}
}
home.html: home.html的:
<ion-header>
<menu></menu>
<ion-title>stores</ion-title>
</ion-header>
<ion-content no-padding>
<ion-list class="list-card" *ngFor="let store of storeArray | async let i = index">
<ion-item (click)="openStore(i)" >
<span class="name">{{store.name}}</span> //got it ok
<span class="floor">{{store.floor}}</span>//got it ok
</ion-item>
</ion-list>
</ion-content>
<ion-card ion-fixed id="card" class="big-card" >
<p>{{description}}</p> // cant get this
</ion-card>
Error: Cannot read property 'description' of undefined 错误:无法读取未定义的属性“描述”
Also this is how look my array passed from provider: console screenshot This is ok? 这也是我从提供者传递的数组的外观: 控制台屏幕快照可以吗?
I need to get objects values from array passed from provider by index that I got from ion-list but maybe there are other ways to do this? 我需要从提供程序传递的数组中获取对象值,而该数组是从离子列表获得的,但也许还有其他方法可以做到这一点?
Your getStoresRemote
method returns a promise and to resolve that promise you are using angular's async
pipe inside the template. 您的
getStoresRemote
方法返回一个Promise,并使用模板内部的angular async
管道来解决该async
。 So the storeArray
resolves only inside the template which is not accessible inside the controller. 因此,
storeArray
仅在模板内部解析,而模板在控制器内部无法访问。
When you try to do this.storeArray[i].description
the controller only knows that storeArray
is a promise (it doesn't know that it actually resolved inside the template), hence throws the error Cannot read property 'description' of undefined
. 当您尝试执行
this.storeArray[i].description
,控制器仅知道storeArray
是一个storeArray
(它不知道它实际上已在模板中解析),因此引发错误Cannot read property 'description' of undefined
。
To solve this problem, you have to either remove async
pipe from template or pass entire store
object into openstore
method instead of i
. 要解决此问题,您必须从模板中删除
async
管道,或者将整个store
对象传递给openstore
方法而不是i
。
Solution 1 解决方案1
home.ts
openStore(i) {
document.getElementById('main').className += ' no-scroll';
document.getElementById('card').className += ' active';
this.buttonClicked = !this.buttonClicked;
this.description = this.storeArray[i].description;
console.log(this.description);
}
ionViewDidLoad() {
this.storesProvider.getStoresRemote().then(data => this.storeArray = data);
}
home.html
<ion-list class="list-card" *ngFor="let store of storeArray let i = index">
<ion-item (click)="openStore(i)" >
<span class="name">{{store.name}}</span>
<span class="floor">{{store.floor}}</span>
</ion-item>
</ion-list>
Solution 2 解决方案2
home.html
<ion-list class="list-card" *ngFor="let store of storeArray | async let i = index">
<ion-item (click)="openStore(store)" >
<span class="name">{{store.name}}</span> //got it ok
<span class="floor">{{store.floor}}</span>//got it ok
</ion-item>
</ion-list>
then change openstore()
method like this 然后像这样更改
openstore()
方法
openStore(store) {
document.getElementById('main').className += ' no-scroll';
document.getElementById('card').className += ' active';
this.buttonClicked = !this.buttonClicked;
this.description = store.description;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.