简体   繁体   中英

Pass Storage value to object Ionic 2

I get value from storage in ionic 2.

        this.storage.get('name').then((nama) => {
            this.name = nama
        });

and i want pass the data "this.name" to the object, but when i run the app, it's not return anything

    this.userData = {
        id: '0404040404',
        nama: this.name,
        no_hp: '082211590346',
        email: 'rifqyzackya@gmail.com'
    }

here my full code

name:any
userData: {id: string, nama: string, no_hp: string, email: string}

constructor(public navCtrl: NavController,public storage: Storage) {

        this.storage.get('name').then((nama) => {
            this.name = nama
        });

    this.userData = {
        id: '0404040404',
        nama: this.name,
        no_hp: '082211590346',
        email: 'rifqyzackya@gmail.com'
    }
}

userData should be initiated in the promise result:

constructor(public navCtrl: NavController,public storage: Storage) {

        this.storage.get('name').then((nama) => {
            this.name = nama
            this.userData = {
                id: '0404040404',
                nama: nama,
                no_hp: '082211590346',
                email: 'rifqyzackya@gmail.com'
            }
        });


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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