title: Calling a method in the component from a Javascript - Angular2
body: <p>I need to call a method declared in my Angular component from a Javascript I have written inside the constructor of the same component. My target is to detect the change of a file input filed and upload the file to the server through a service.</p>
  
  <p>I have added this declaration after the imports.</p>
  
  <pre><code>declare var $: any;
  </code></pre>
  
  <p>In my constructor, I have written following code.</p>
  
  <pre><code>$(document.body).on("change.bs.fileinput", ".fileinput-exists", function () {
  
      console.log($(this).parent().find('.myImage').val());
      let input = $(this).parent().find('.myImage');
      this._photoToBeSaved = input.prop('files')[0];
      console.log("my file = " + this._photoToBeSaved);
      //upload the image
      () =&gt; {
          this.uploadPhoto(this._photoToBeSaved);
      }
  });
  </code></pre>
  
  <p>Following is the <code>uploadPhoto</code> method in the same component.</p>
  
  <pre><code>uploadPhoto(photo: any) {
      console.log("here i am at upload method);
      // call service method to upload
      .....
  }
  </code></pre>
  
  <p>However, this does not work. </p>
  
  <p>Note that for the following part in the constructor, I have followed the answer for <a href="https://stackoverflow.com/questions/41329505/angular2-call-component-method-from-javascript-function">this</a> question. Can I really do that. Is it correct?</p>
  
  <pre><code>() =&gt; {
     this.uploadPhoto(this._photoToBeSaved);
  }
  </code></pre>
  
  <p>the <code>uploadPhoto()</code> method does not get called. Why is that? How to correct this?</p>