简体   繁体   English

如何从Angular中的另一个函数调用一个函数?

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

onFileSelected(event) is called when file is selected from input type (from Home.html ), everything works fine except when I try to call this.callproducts(data,0,c_altcode) . onFileSelected(event)当从输入类型(从选定的文件被称为Home.html ),一切工作正常,除了当我尝试打电话this.callproducts(data,0,c_altcode)

Below is my home.ts code, where am I going wrong?下面是我的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
}

Error coming when I am trying to call this function which is outside of onFileSelected function.当我尝试调用onFileSelected函数之外的这个函数时出现错误。 The error is:错误是:

[ts] Property 'callproducts' does not exist on type 'XMLHttpRequest' [ts] 类型“XMLHttpRequest”上不存在属性“callproducts”

"this" is equal to "oReq" if I am correct.如果我是对的,“this”等于“oReq”。 I don't believe that "this."我不相信“这个”。 Is right.是对的。 Try just callProducts without this.尝试只调用没有这个的产品。

You cannot call this.callproducts(data,0,c_altcode) inside another function because it is not in your component's scope.您不能在另一个函数中调用this.callproducts(data,0,c_altcode)因为它不在您的component's范围内。

But you can try below.但是你可以在下面试试。

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