admin管理员组文章数量:1417464
I am trying to set the value retrieved from .get function into a variable declared outside but unable to do so.
var dt;
//retrieve
this.local.get('didTutorial').then((value) => {
alert(value);
dt = value;
})
console.log("Local Storage value: "+dt);
I'm able to get "true" for the alert, but getting "undefined" for the console.log that is printing outside of the function.
One workaround is that I can put all my remaining codes into the ".then function" , but that would be very messy.
Update(Solution):
As per ionic api (/) , they use .get to retrieve values.
Since using promises has it's own limitations, by using the following:
constructor(navController) {
this.navController = navController;
this.local = new Storage(LocalStorage);
}
and getItem function,
localStorage.getItem('didTutorial')
You will be able to retrieve it without having to put everything into the callback method.
I am trying to set the value retrieved from .get function into a variable declared outside but unable to do so.
var dt;
//retrieve
this.local.get('didTutorial').then((value) => {
alert(value);
dt = value;
})
console.log("Local Storage value: "+dt);
I'm able to get "true" for the alert, but getting "undefined" for the console.log that is printing outside of the function.
One workaround is that I can put all my remaining codes into the ".then function" , but that would be very messy.
Update(Solution):
As per ionic api (http://ionicframework./docs/v2/api/platform/storage/LocalStorage/) , they use .get to retrieve values.
Since using promises has it's own limitations, by using the following:
constructor(navController) {
this.navController = navController;
this.local = new Storage(LocalStorage);
}
and getItem function,
localStorage.getItem('didTutorial')
You will be able to retrieve it without having to put everything into the callback method.
Share Improve this question edited Apr 25, 2016 at 7:11 Gene asked Apr 25, 2016 at 3:35 GeneGene 2,2184 gold badges31 silver badges52 bronze badges 2-
Hey, this evaluates to:
Error TS2339: Property 'getItem' does not exist on type 'Storage'.
– Thomas Modeneis Commented Aug 26, 2016 at 12:32 - @ThomasModeneis I might not be sure as this is for .beta5 and lower. – Gene Commented Sep 7, 2016 at 8:39
1 Answer
Reset to default 6Reading from your localStorage wrapper in this case is asynchronous, which means that the callback passed to this.local.get
gets called after your call to console.log
. Try placing console.log
inside your callback; it should work then:
// retrieve
this.local.get('didTutorial').then((value) => {
alert(value)
var dt = value
console.log("Local Storage value:", dt)
})
Also, you'll notice that I changed your console.log
call arguments. That's because console.log
accepts 1 or more parameters, and formats them much more nicely when you pass them in instead of concatenating them. Just a pro tip.
本文标签: javascriptionic 2 local storage unable to set retrieved value to variableStack Overflow
版权声明:本文标题:javascript - ionic 2 local storage unable to set retrieved value to variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745262337a2650411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论