【发布时间】:2019-02-07 05:21:27
【问题描述】:
如何在 Angular 2 中调用同一控制器中的函数,因为这会给我一个错误:
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();
【问题讨论】:
-
你能发布完整的功能吗?您可能在某处缺少支架。顺便说一句,您的意思是递归调用该函数吗?
-
是的,我刚刚添加了完整的功能。不,我不是要递归调用它,它是一个初始化函数,我只想在开始时调用它。
-
那么你想从哪里调用它?
ngOnInit?
标签: angular typescript ionic2