簡體   English   中英

如何從Angular中的另一個函數調用一個函數?

[英]How to call a function from another function in Angular?

onFileSelected(event)當從輸入類型(從選定的文件被稱為Home.html ),一切工作正常,除了當我嘗試打電話this.callproducts(data,0,c_altcode)

下面是我的home.ts代碼,我哪里出錯了?

 onFileSelected(event)
  {
    var file = event.target.files[0];
        ...// some code here
    oReq.onload = function(e) {
        ...// some code here

if(oReq.status === 200)
          { 
            if(final_arr[0].hasOwnProperty("Altcode"))
            {
              var c_altcode =final_arr[0].hasOwnProperty("Altcode");

         // error coming in below line when i am trying to call this function which is outside `onFileSelected` function. 
          this.callproducts(data,0,c_altcode);           
                }

          }
        }

        oReq.send(null);
  }


callproducts(a,b,c,d){
...//some code here
}

當我嘗試調用onFileSelected函數之外的這個函數時出現錯誤。 錯誤是:

[ts] 類型“XMLHttpRequest”上不存在屬性“callproducts”

如果我是對的,“this”等於“oReq”。 我不相信“這個”。 是對的。 嘗試只調用沒有這個的產品。

您不能在另一個函數中調用this.callproducts(data,0,c_altcode)因為它不在您的component's范圍內。

但是你可以在下面試試。

export class HomePage {

  constructor() {}

 onFileSelected(event) {

    let file = event.target.files[0];

    oReq.onload = function(e) {

        if(oReq.status === 200) { 

            if(final_arr[0].hasOwnProperty("Altcode")) {

              let c_altcode =final_arr[0].hasOwnProperty("Altcode");
              HomePage.callproducts(data,0,c_altcode);           
            }
          }
        }
        oReq.send(null);
  }

static callproducts(a,b,c,d){

  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM