如何在同一个控制器中调用一个函数
How to call a function within the same controller
如何在 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();
您不能在一个方法中调用一段代码,因此您应该在另一个方法中调用 this.initialiseWeight()
,例如:
ngOnInit() {
this.initialiseWeight();
}
您应该在 ngOnInit()
中调用 this.initialiseWeight();
进行初始化,或者您可以从另一个函数调用它 someFunction()
ngOnInit(){
this.initialiseWeight();
}
或
someFunctionName(){
this.initialiseWeight();
}
如何在 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();
您不能在一个方法中调用一段代码,因此您应该在另一个方法中调用 this.initialiseWeight()
,例如:
ngOnInit() {
this.initialiseWeight();
}
您应该在 ngOnInit()
中调用 this.initialiseWeight();
进行初始化,或者您可以从另一个函数调用它 someFunction()
ngOnInit(){
this.initialiseWeight();
}
或
someFunctionName(){
this.initialiseWeight();
}