Navigating and transferring data between Ionic 2 pages

I'm having some problems navigating between pages with Ionic2.

I have a list of data that I got from data.json

Here is a list

enter image description here

I want to get the details:

Example - from "A"

The data that I have on my data is /Example.Json

 {
  "title": "A",
  "lat": 2.323733,
  "lon": 64,546465
},

So, as soon as I click on Title, I want to get the title, lat and lon on a new page.

app.modules.ts

    import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { MapPage } from '../pages/map/map';
import { ListPage } from '../pages/list/list';
import { DetailPage } from '../pages/detail/detail';
import { Locations } from '../providers/locations';
import { GoogleMaps } from '../providers/google-maps';
import { Connectivity } from '../providers/connectivity';
declare var google: any;

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    MapPage,
    ListPage,
    DetailPage
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    MapPage,
    ListPage,
    DetailPage
  ],
  providers: [{provide: ErrorHandler, useClass: IonicErrorHandler},Locations,GoogleMaps, Connectivity]
})
export class AppModule {}

    import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { MapPage } from '../pages/map/map';
import { ListPage } from '../pages/list/list';
import { DetailPage } from '../pages/detail/detail';
import { Locations } from '../providers/locations';
import { GoogleMaps } from '../providers/google-maps';
import { Connectivity } from '../providers/connectivity';
declare var google: any;

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    MapPage,
    ListPage,
    DetailPage
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    MapPage,
    ListPage,
    DetailPage
  ],
  providers: [{provide: ErrorHandler, useClass: IonicErrorHandler},Locations,GoogleMaps, Connectivity]
})
export class AppModule {}

PageA.ts (data list)

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Locations } from '../../providers/locations';
import { DetailPage } from '../detail/detail';


@Component({
  selector: 'page-list',
  templateUrl: 'list.html'
})
export class ListPage {


  constructor(public navCtrl: NavController, public locations: Locations) {

  }

  ionViewDidLoad() {
    console.log('Test Si marche');


  }

  viewLocation(event,location) {
    this.navCtrl.push(DetailPage, {
      "title":location.title,
      "longitude":location.longitude
    })
  }

}

Page B (where I want to receive the item as soon as I click on something in the list)

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {NavParams} from "ionic-angular";
import { Locations} from '../../providers/locations';
import { ListPage } from '../list/list';





@Component({
    selector: 'page-detail',
    templateUrl: 'detail.html',
    providers: [ Locations ],
    entryComponents:[ ListPage ]
})


export class DetailPage {

    title: string
    latitude: string
    navParams: NavParams

    constructor(navParams: NavParams,public navCtrl: NavController) {
        this.navParams = navParams
        this.title = this.navParams.get('title');
        this.latitude = this.navParams.get('latitude');
    }

    ionViewDidLoad(err) {
        console.log(err);


    }
    goBack() {
        this.navCtrl.pop();
    }

}

listA.html

 <ion-header>

    <ion-navbar color="secondary">
        <ion-title>List</ion-title>
    </ion-navbar>

</ion-header>


<ion-content>

    <ion-list no-lines>

        <ion-item *ngFor="let location of locations.data">

            <ion-item (click)="viewLocation($event, locations)">{{locations.title}}</ion-item>
            <ion-avatar item-left>
                <ion-icon name="pin"></ion-icon>
            </ion-avatar>
            <!--<img src="./src/pics/mus.png"/>-->
            <h2>{{location.title}}</h2>
            <p>{{location.distance*1.609}} Km</p>
        </ion-item>

    </ion-list>

</ion-content>

data.json

  [
  { "title": "Cat", "longitude": "14,57" },
  { "title": "Bird", "longitude": "17.45" },
  { "title": "Spider", "longitude": "19,12" }
]
+4
source share
1 answer

If you want to transfer data from one page to another, you can

1) JSON SQL JSON.stringify(store) JSON.parse().

, , SQL , JSON.parse, JSON.

, SQL. SQL Ionic 2

JSON Parse

JSON Stringify

, , 2) navController "push" . , JSON , .

:

this.navController.push(ThePageNameYouWannaGo, {
      firstPassed: "firstData",
      secondPassed: "secondData",
      thirdPassed: "thirdData",
});


//On the next page (at your constructor),

Constructor(....,NavParams,.....){

  this.first = navParams.get("firstPassed");
  this.second= navParams.get("secondPassed");
  this.third= navParams.get("thirdPassed");

//the value would be stored inside this.first, this.second respectively 

1, , JSON JSON.

+4

Source: https://habr.com/ru/post/1673836/


All Articles