2016-09-27 93 views
0

我在Ionic上写我的第一个应用程序。这是一个简单的笔记应用程序。我救了我的笔记列表如下:如何在将两个变量存储到localStorage时连接两个变量?

save() { 
    if(this.todoItem != "" && this.todoText != "") { 
     this.todoList.push(this.todoItem); 
     localStorage.setItem("todol", JSON.stringify(this.todoList)); 
     this.nav.pop(); 
    } 
} 

我想要的TodoItem和todoText连接,所以当我把它推到本地存储JSON会是这个样子:

{"todoList":[ 
    {"todoItem":"item1", "todoText":"text1"}, 
    {"todoItem":"item2", "todoText":"text2"}, 
    {"todoItem":"item3", "todoText":"text3"} 
]} 

回答

0

代替把普通的todoItem推到列表中,你可以尝试用你需要的模式形成一个Object,如图所示。

save() { 
    if(this.todoItem != "" && this.todoText != "") { 

     this.todoList.push({"todoItem":this.todoItem , "todoText":this.todoText}); 
     localStorage.setItem("todol", JSON.stringify(this.todoList)); 
     this.nav.pop(); 
    } 
} 
+0

我应该忽略这个错误:''参数类型'{“todoItem”:string; “todoText”:string; }'不能分配给类型'string''的参数? – DaimCho

+0

也许这样? 'this.todoList.push({“todoItem”:this.todoItem,“todoText”:this.todoText}:any);' – sneeky

+0

@sneeky现在'','expected。' – DaimCho