我从本地存储中获取值并将其传递给模态。代码中的代码console.log(obj);
行打印obj
的初始值,{selectedRadious: 500, DistanceUnit: 'Meters'}
为this.storage.get()
,因为let obj = {selectedRadious: 500, DistanceUnit: 'Meters'};
this.storage.get('lastSetValue').then((val) => {
if(val.selectedRadious){
this.selectedRadious = val.DistanceUnit;
}
if(val.DistanceUnit){
this.DistanceUnit = val.DistanceUnit;
}
obj = {selectedRadious: this.selectedRadious, DistanceUnit: this.DistanceUnit};
})
.catch(err=>{
});
console.log(obj);
let myModal = this.modalCtrl.create(SettingModalPage, obj);
myModal.onDidDismiss(data => {
console.log('modal value: '+data.DistanceUnit)
this.DistanceUnit = data.DistanceUnit;
this.selectedRadious = data.selectedRadious;
let objValue = {selectedRadious: this.selectedRadious, DistanceUnit: this.DistanceUnit};
this.storage.set('lastSetValue', objValue);
});
myModal.present();
是异步函数。如何处理它以获得本地存储价值?
var tableHtml = "<table class="table table-striped" id="tableSubs"><thead><tr><th>PROFILE ID</th><th>Status</th><th>Last Time Paid</th><th>Next Payment</th><th>Billing Cycles</th><th>Total Billing Cycles</th><th>Total Amount Paid</th></tr></thead><tbody>";
$.each(data, function (idx, obj) {
tableHtml += "<tr>";
tableHtml += "<td>" + obj.ProfileID + "</td>";
tableHtml += "<td>" + obj.Status + "</td>";
tableHtml += "<td>" + obj.LastTimePaid + "</td>";
tableHtml += "<td>" + obj.NextTimePayment + "</td>";
tableHtml += "<td>" + obj.BillingCycle + "</td>";
tableHtml += "<td>" + obj.TotalCyclesCompleted + "</td>";
tableHtml += "<td>" + obj.TotalAmountPaid + "</td>";
tableHtml += "</tr>";
});
tableHtml += "</tbody></table>";
$("#TABLEID").html(tableHtml);
答案 0 :(得分:2)
我在这里已经回答:Can't resolve all parameters for Storage: (?)
您需要将访问已解决的承诺中的值的代码移动到该承诺的.then()
。因为它是异步操作,所以当你使用它时,它可能不可用。
E.g。这可行:
presentModal() {
this.storage.get('distUnit').then((val) => {
console.log('Your distUnit is', val);
this.DistanceUnit = val;
this.storage.get('SetRadious').then((val) => {
console.log('Your SetRadious is', val);
this.selectedRadious = val;
this.modalCreator();
})
.catch(err=>{
console.log('Your SetRadious dont exist: ' + JSON.stringify(err));
this.selectedRadious = 500;
this.modalCreator();
});
})
.catch(err=>{
console.log('Your distUnit dont exist: ' + JSON.stringify(err));
this.DistanceUnit = 'Meters';
this.storage.get('SetRadious').then((val) => {
console.log('Your SetRadious is', val);
this.selectedRadious = val;
this.modalCreator();
})
.catch(err=>{
console.log('Your SetRadious dont exist: ' + JSON.stringify(err));
this.selectedRadious = 500;
this.modalCreator();
});
});
}
modalCreator(){
let obj = {selectedRadious: this.selectedRadious, DistanceUnit: this.DistanceUnit};
let myModal = this.modalCtrl.create(SettingModalPage, obj);
myModal.onDidDismiss(data => {
console.log('modal value: '+data.DistanceUnit)
this.DistanceUnit = data.DistanceUnit;
this.selectedRadious = data.selectedRadious;
this.storage.set('distUnit', this.DistanceUnit);
this.storage.set('SetRadious', this.selectedRadious);
});
myModal.present();
}
如果您仔细阅读代码,您将了解我已经处理了获取这两个参数以及其中一个参数失败的所有情况。看看,告诉我。