Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ionic2在存储器中存储对象数组_Javascript_Angular_Ionic Framework - Fatal编程技术网

Javascript ionic2在存储器中存储对象数组

Javascript ionic2在存储器中存储对象数组,javascript,angular,ionic-framework,Javascript,Angular,Ionic Framework,我在ionic2存储中遵循以下示例: 我从表单页面获取一个对象参数,将其添加到myBOTs变量并保存整个对象数组,以便在页面刷新时使用它填充列表 我在构造函数中所做的只是调用存储来查找当前值以填充列表 id: any; icons: string[]; myBOTs: Array <{type: string,BOTdate: string, setReminder: boolean, icon: string}>; constructor(public navCtrl: NavC

我在ionic2存储中遵循以下示例:

我从表单页面获取一个对象参数,将其添加到myBOTs变量并保存整个对象数组,以便在页面刷新时使用它填充列表

我在构造函数中所做的只是调用存储来查找当前值以填充列表

id: any;
icons: string[];
myBOTs: Array <{type: string,BOTdate: string, setReminder: boolean, icon: string}>;

constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage) {
    this.myBOTs=[];
    this.storage.get('myBOTs').then((values)=>{
        if(values!==null){
            for(let i = 0;i < values.length;i++){
                this.myBOTs.push({
                    type: values[i].type,
                    BOTdate: values[i].BOTdate,
                    icon: this.icons[Math.floor(Math.random() * this.icons.length)],
                    setReminder: values[i].setReminder
                })
            }               
        }
    })
}
我已经解决了

问题在于当页面加载时,构造函数和ionViewDidLoad都会被异步调用。这将导致表单页面中的数据推送到This.myBOTs中,然后从构造函数列表中的存储中获取和加载结果

这就是我的解决方案。我所做的是将代码添加到函数中,并在表单数据添加到列表之前向列表生成函数中添加回调

id: any;
icons: string[];
myBOTs: Array <{type: string,BOTdate: string, setReminder: boolean, icon: string}>;

constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage) {
    this.myBOTs=[];
    let e = this;
    this.generate_list(function(){
        e.add_item();
    })
}

很完美。这可能会帮助某些人,因为这是一种非常常见的情况。

您在哪里初始化这个.myBOTs?这是一个空白数组吗?当你按下它时,你确定它在这个数组中有一个先前的值吗?顺便说一句,this.storage.set是异步的,正如get一样。因此,您不能保证console.log(值)在set方法完成后发生。是的。我注意到storage.set是异步的。但是是的,刷新后的结果仍然只是数组的最后一个对象。这是行吗?myBOTs:Array;这行呢?:this.myBOTs=[];如果存储中有值,它将使用.get添加到构造函数中。如果不是,它将只是一个空数组。
id: any;
icons: string[];
myBOTs: Array <{type: string,BOTdate: string, setReminder: boolean, icon: string}>;

constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage) {
    this.myBOTs=[];
    let e = this;
    this.generate_list(function(){
        e.add_item();
    })
}
generate_list(callback){
    this.storage.get('myBOTs').then((values)=>{
        if(values!==null){
            for(let i = 0;i < values.length;i++){
                this.myBOTs.push({
                    type: values[i].type,
                    BOTdate: values[i].BOTdate,
                    icon: this.icons[Math.floor(Math.random() * this.icons.length)],
                    setReminder: values[i].setReminder
                })
            }               
        }
        callback();
    })
}
add_item(){
    if(this.navParams.get('type')!==undefined){
        let addedBOT = [];
        addedBOT['type'] = this.navParams.get('type');
        addedBOT['BOTdate'] = this.navParams.get('BOTdate');
        addedBOT['setReminder'] = this.navParams.get('setReminder');
        this.myBOTs.push({
            type: addedBOT['type'],
            BOTdate: addedBOT['BOTdate'],
            icon: this.icons[1],
            setReminder: addedBOT['setReminder']
        })
        this.storage.set('myBOTs',this.myBOTs);
        console.log('storage added');        
    }    
}