Angular 如何在同一控制器内调用函数

Angular 如何在同一控制器内调用函数,angular,typescript,ionic2,Angular,Typescript,Ionic2,如何在Angular 2中调用同一控制器内的函数,因为这会给我一个错误: initialiseWeight(){ this.storage.ready().then(() => { return this.storage.get('weightunit'); }) .then(retrievedUnit => { if (retrievedUnit) { this.data.weightunit = re

如何在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();
initialiseWeight(){
this.storage.ready()。然后(()=>{
返回此.storage.get('weightunit');
})
.然后(检索单位=>{
if(检索单元){
this.data.weightunit=检索单位;
}否则{
this.data.weightunit='kg';
this.storage.set('weightunit',this.data.weightunit);
}
})
.catch(e=>console.error('错误:无法读取存储重量,错误:',e));
this.storage.ready()。然后(()=>{
返回此.storage.get('realWeight');
})
。然后(retrievedRealWeight=>{
如果(retrievedRealWeight&&retrievedRealWeight!=“0”){
this.data.realWeight=parseInt(retrievedRealWeight,10);
this.storage.get('userWeight')。然后((值)=>{
this.data.userWeight=parseInt(值,10);
});
如果((this.data.realWeight*1.02console.error('错误:无法读取存储重量,错误:',e));
}
这个.initialiseWeight();

你不能在一个方法中调用一段代码,所以你应该在另一个方法中调用
this.initialiseWeight()
,例如:

ngOnInit() {
    this.initialiseWeight();
}

你不能在一个方法中调用一段代码,所以你应该在另一个方法中调用this.initialiseWeight(),例如:

ngOnInit() {
    this.initialiseWeight();
}

您应该调用
this.initialiseWeight()
ngOnInit()
中进行初始化,或者您可以从另一个函数调用它
someFunction()


您应该调用
this.initialiseWeight()
ngOnInit()
中进行初始化,或者您可以从另一个函数调用它
someFunction()


你能发布完整的功能吗?你可能在什么地方漏掉了一个支架。顺便问一下,你的意思是递归调用那个函数吗?是的,我刚刚添加了完整的函数。不,我不是要递归调用它,它是一个初始化函数,我只想在开始时调用它。那么你想从哪里调用它呢
ngOnInit
?您能发布完整的功能吗?你可能在什么地方漏掉了一个支架。顺便问一下,你的意思是递归调用那个函数吗?是的,我刚刚添加了完整的函数。不,我不是要递归调用它,它是一个初始化函数,我只想在开始时调用它。那么你想从哪里调用它呢<代码>恩戈尼特
someFunctionName(){
   this.initialiseWeight();
}