简体   繁体   English

ionic2-无法使用navcontroller检索/显示数据

[英]ionic2 - unable to retrieve/display data using navcontroller

I am unable to display the data I get from NavParams . 我无法显示从NavParams获得的数据。 I use console.log() and check that I did get the data I wanted but unable to display in the new page. 我使用console.log()并检查是否确实获得了所需的数据,但无法在新页面中显示。

I think I might had make some mistake during the passing of data, but not sure what did i do wrong. 我认为我在传递数据时可能犯了一些错误,但是不确定我做错了什么。

Any help is appreciated. 任何帮助表示赞赏。 Thanks in advanced. 提前致谢。

first.ts first.ts

import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { LocationsProvider } from '../../providers/locations/locations';

...

    constructor(
        public locations: LocationsProvider,
        public viewCtrl: ViewController,
        public navCtrl: NavController, 
        public actionSheetCtrl: ActionSheetController,
        public http: Http,
        public navParams: NavParams,
    ) { }

    newEntry(param){
        this.navCtrl.push('SecondPage',param);
    }

    openActSheetjob_Type(){
        let actionsheet = this.actionSheetCtrl.create({
            title:"Type",
            buttons:[
                {
                    text: 'Hour',
                    handler: () => {
                        let Hourly = "Hourly";  
                        let results =  this.locations.getData(Hourly);

                        console.log(results); // data goten

                        this.newEntry({ record: results });   //Suspect mistake
                    }
                }
            ]
        });
        actionsheetjob_type.present();
    }

Second.html Second.html

<ion-list >
    <ion-item *ngFor="let list of this.results">
        <h2>{{ list.Title }}</h2>
        <p>{{ list.Type }}</p>
    </ion-item>
</ion-list>

Second.Ts Second.Ts

ionViewDidLoad(){
    console.log(this.NP.get("record")); //Get NULL
}

locations.ts locations.ts

//function to get data
data:any;

getData(jobType){
    if (this.data) {
        return Promise.resolve(this.data);
    }
    return new Promise(resolve => {
        this.http.get('http://localhost/getType.php?Type=' + Type)
        .map(res => res.json())
        .subscribe(data => {
            this.data = data;
            console.log(this.data);
            resolve(this.data);
        });
    });
}

Since getData returns a Promise it is asynchronous. 由于getData返回Promise因此它是异步的。 console.log(results) and this.newEntry({ record: results }) will run before results has a value. console.log(results)this.newEntry({ record: results })将在results具有值之前运行。 And with your code it will not even have the value you think. 而使用您的代码,它甚至将不会具有您认为的价值。 You can get the value in the then function that will be called when the Promise resolves. 您可以在Promise解析时将被调用的then函数中获取值。

handler: () => {
    let Hourly = "Hourly";  
    this.locations.getData(Hourly)
    .then(results => {
        console.log(results);
        this.newEntry({ record: results });
    }
}

in this.navCtrl.push('SecondPage',param); this.navCtrl.push('SecondPage',param); shoud SecondPage import in first.ts file like this should be import{ SecondPage } from '../second/second' and its class name gives to this.navCtrl.push(SecondPage,param); 应该将firstPage.ts文件中的SecondPage导入应该import{ SecondPage } from '../second/second' ,其类名将赋予this.navCtrl.push(SecondPage,param);

Hope this should be help! 希望对您有所帮助!

I am just writing what is working in my case, may be it help you. 我只是在写对我而言有效的方法,可能对您有帮助。

 this.navCtrl.push(SecondPage,{item : xyz});//In First Page

and in constructor of second page us 在第二页的构造函数中

  this.item = params.data.item;// In second page

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM