【发布时间】:2019-02-11 02:19:48
【问题描述】:
我正在从事 ionic 4 项目。我的项目是使用输入搜索从 url 获取数据 json。我希望在用户单击任何项目时传递数据以向他们显示另一个页面以获取更多详细信息。我尝试使用此代码,但我得到未定义的值!
这段代码正在进行搜索操作
export class FlightsearchPage {
public search = ''
public flight : any
filterItems:any;
callsign : any;
constructor(private http: HTTP, public loadingController: LoadingController, private nav : NavController) {
this.addThemFunction
}
keyPressed(event: any) { /
console.log(event.target.value);
this.addThemFunction(event.target.value);
}
async addThemFunction(search){
this.http.get('/web/find?query='+search+'', {}, {})
.then(data => {
const parsed = JSON.parse(data.data);
this.flight = parsed.results;
this.filterItems= this.flight;
this.callsign = this.flight.detail.flight
/// here l am try to pass value callsign to page details ///
this.nav.navigateForward(`/flightserachdetails/${this.callsign}`)
}), err=>{
}
}
}
详情搜索
<ion-content padding>
<ion-item>
<ion-label position="floating">Enter your flight number</ion-label>
<ion-input [(ngModel)]="search" (keyup)="keyPressed($event)" placeholder="Ex : iaw556"></ion-input>
</ion-item>
<ion-item *ngFor="let item of flight" routerDirection="forward">
<ion-label>
<ion-text color="primary">
<h3 [routerLink]="['/flightserachdetails', id]">{{item.label}}</h3>
</ion-text>
<p>{{item.name}}</p>
</ion-label>
</ion-item>
</ion-content>
这段代码正在从上面的代码中获取数据
export class FlightserachdetailsPage {
public flight : any
callsignId = null
constructor(private http: HTTP, public loadingController: LoadingController, private nav: NavController,private activatedRoute: ActivatedRoute) {
this.callsignId = this.activatedRoute.snapshot.paramMap.get('id')
}
用于显示来自上面另一个页面的数据。
<ion-content padding>
{{callsignId}}
{{flight.request.fetchBy}}
</ion-content>
json 响应
{
"results": [
{
"id": "IA107",
"label": "IA107",
"detail": {
"callsign": "IAW107",
"flight": "IA107",
"fetchBy": "IA107"
},
"type": "schedule",
}
]
}
我很抱歉混乱代码。请给我任何建议或给我另一个代码示例?
【问题讨论】:
-
在现有服务中编写服务或方法/属性以通过其 id 获取对象/项目,并在导航到下一页时将对象/项目的 id 传递到 url 并调用此服务方法在下一页上加载的组件中通过 id 获取该对象。我认为这更容易。