javascript - Calling a function in the page constructor results in empty array -
this question has answer here:
- how return response asynchronous call? 21 answers
i try load , bind this.studentnames[i].checked
prepare checkboxes (key-value pair in localstorage if checkbox set). however, after correctly pushing entries storage in this.studentnames
array (i can see of them in storage in view), this.studentnames array empty [] in array , hence not longer accessable model.
do use here in wrong way? thought, when use =>
functions, erverything should ok?!
my view (html):
<ion-list> <ion-item *ngfor="let studentname of studentnames; let i=index"> <ion-label>{{studentname.name}}</ion-label> <ion-checkbox [(ngmodel)]="studentname.checked" (ionchange)="checkboxtoggle($event,studentname)"></ion-checkbox> </ion-item> </ion-list>
my model (typescript):
@mycomp... selecteditem: any; studentnames: array<{id: string, name: string, checked: boolean}>; constructor(private navparams: navparams, public storage: storage) { // if navigated page, have item available nav param this.selecteditem = navparams.get('item'); this.studentnames = []; this.readstorageatstartup(); console.log(this.studentnames) } public readstorageatstartup() { this.storage.ready().then(() => { this.storage.foreach( (value: string, key: string, index: number) => { if(key.charat(0)=="s") { this.studentnames.push({id: key, name: value, checked: true}) } }) }) }
console.log() (firefox):
array [ ]
thanks help!
pass storage parameter readstorageatstartup function , resolve asynchronous request there
public readstorageatstartup(storage: storage) { storage.ready().then(() => { storage.foreach( (value: string, key: string, index: number) => { if(key.charat(0)=="s") { this.studentnames.push({id: key, name: value, checked: true}) } }) })
}
Comments
Post a Comment