-1

On the file app-component.html there is a button:

<button type="button" onclick="confirmChanges()">Save Changes</button>

This confirmChanges calls a javascript function located on my index.html file:

<script>
    function confirmChanges(){
      var optionSelected = confirm('Are you sure?');
      if(optionSelected == true){
        alert('Changes were applied sucessfully!');
        updateAddress();
      } else{
        alert('You discarded the changes!');
      }
    } 
  </script>

The updateAddress() function its on my app-component.ts file. I would like to know how I could call a typescript function from a javascript function without getting Uncaught ReferenceError. Thank you.

2

2 Answers 2

1

Yes you can, as long as your typescript is correctly compiled in Javascript, and your function is exported in the global scope.

But I have to say, it seems wrong to do that in so many way. First exposing something in the global scope is never a good idea. Second, I don't see why you would want to wrote pure javascript scripts in an Angular application.

But yes, technically, it's possible to do what you want, event though I strongly advise against it.

Hope that helps

Sign up to request clarification or add additional context in comments.

2 Comments

It's hard to give a helpful answer to subpar question. This one is more like verbose comment rather than answer. It doesn't answer the question.
Thank you for your answer, but can you give and advise on how should I do it?!
0

Think you'll find the answer here.

Uncaught ReferenceError: <function> is not defined at HTMLButtonElement.onclick

Short answer is you need to add it to global scope. E.g add it to index.html and it should fire. But I recommend that you add it to your .ts-file and rewrite the function

Comments