Reputation: 2608
How to call a function within the same controller in Angular 2, because this gives me an error:
initialiseWeight(){
this.storage.ready().then(() => {
return this.storage.get('weightunit');
})
.then(retrievedUnit => {
if (retrievedUnit) {
this.data.weightunit = retrievedUnit;
} else {
this.data.weightunit = 'kg';
this.storage.set('weightunit', this.data.weightunit);
}
})
.catch(e => console.error('ERROR: could not read Storage Weight, error:', e));
this.storage.ready().then(() => {
return this.storage.get('realWeight');
})
.then(retrievedRealWeight => {
if (retrievedRealWeight && retrievedRealWeight!== "0" ) {
this.data.realWeight = parseInt(retrievedRealWeight, 10);
this.storage.get('userWeight').then((value) => {
this.data.userWeight = parseInt(value, 10);
});
if ( (this.data.realWeight * 1.02 < this.data.userWeight) && (!this.data.isWetsuit) ) {
this.data.userWeight = this.data.realWeight;
this.storage.set('userWeight', this.data.userWeight);
}
} else {
this.data.realWeight = 70;
this.data.userWeight = 70;
this.storage.set('realWeight', this.data.realWeight);
this.storage.set('userWeight', this.data.userWeight);
}
})
.catch(e => console.error('ERROR: could not read Storage Weight, error:', e));
}
this.initialiseWeight();
Upvotes: 0
Views: 50
Reputation: 9687
you should call this.initialiseWeight();
in ngOnInit()
for initialization purposes or you can call it from another function someFunction()
ngOnInit(){
this.initialiseWeight();
}
or
someFunctionName(){
this.initialiseWeight();
}
Upvotes: 1
Reputation: 8868
You can't call a piece of code within a method so you should call this.initialiseWeight()
inside another method, for exemple:
ngOnInit() {
this.initialiseWeight();
}
Upvotes: 1