Invoke a TypeScript function within JavaScript code

As a result of the limitations of IE10 and earlier versions, I find myself in need to reimplement the Import/Upload functionality that I had initially created since FileReader is not supported. After some consideration, I have opted to utilize an iFrame in order to fulfill the requirement for uploading with older browsers.

Below is the structure of my form:

<form id="file_upload_form" method="post" enctype="multipart/form-data" action="upload.php">
    <input name="file" id="file" size="27" type="file" /><br />
    <input type="submit" id="import" value="Import" /><br />
</form>

While working on the JavaScript portion, I realized that calling the action directly would require significant rework. Below is the current progress of my JavaScript code snippet:

$('#import').click(function () {
    $('#imageForm').submit();

    $('#iFrameImage').load(function () {
        $('#file').val('');
        $.get('ImportExportController/Put');
    });
});

In the TypeScript code snippet below, you can observe how I am invoking the action in my controller from within TypeScript. In the past, I could simply use ng-click to trigger this method.

 public Import(xml, parentNode): restangular.IPromise<any> {
        var vr = {
            IsOk: false,
            Data: [xml, somethinghere],
            Errors: []
        };
        return this.restangular.one('Import', '0').customPUT(vr);
    }

I am currently facing the challenge of figuring out how to call this TypeScript function (which serves as my Angular viewmodel) using JavaScript.

The main idea behind this approach is to enable file upload via an iFrame and then upon uploading, invoke the TypeScript function.

Answer №1

When working with Typescript, it is important to remember that it gets compiled into javascript. This means you can interact with it like a regular javascript function. However, the structure may differ slightly from what you are used to. One effective way to understand how it all works is by comparing the original Typescript file with the compiled javascript file. If your Typescript code includes modules or classes, they will influence how you call the function. For example:

class Foo{
    constructor(){}

    public Bar = () : string =>{
        return "This is a string";
    }
}

module FooModule {
    export var FooInstance = new Foo();
}

In both typescript and javascript environments, you would use the following syntax to call the function (assuming the page has been imported correctly).

FooModule.FooInstance.Bar();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to export a specific portion of a destructuring assignment?

const { a, ...rest } = { a: 1, b: 2, c: 3 }; If I want to export only the rest object in TypeScript, how can I achieve that? ...

Having trouble parsing bytes from a protobuf message in JavaScript

In my current project, I am faced with the task of encoding and decoding bytes within a protobuf message using Javascript. It seems that working with strings is functional, but once I introduce bytes in the message definition, retrieving the data becomes a ...

Creating a Dynamic System for Converting Numeric Digits to Alphabetical Grades

Utilizing the code provided below, you can calculate a user's grade point average ranging from A+ to E-. Is there a way, within the following fiddle, to not only display the GPA but also convert it into a corresponding letter grade from A+ to E-? Curr ...

Building an array of objects using a foreach loop

i am struggling to create an array of objects from two input groups, each group consists of 3 inputs with data-side attributes set to left or right every input has a class named "elm" and a data-pos attribute set to a, b, or c <input class="elm-left elm ...

Unable to prolong TypeScript document

As I develop a drag and drop interface, upon dropping a file, the native File is retrieved. To enhance this interface with additional information, I decided to explore various approaches. In my initial attempt, I utilized: interface AcceptedFile extends ...

utilizing firebase.auth() with $scope post user authentication

I have a simple ng-model that I am using to display login/content with AngularJS. Take a look at my facebookAuthProvider: $scope.enterFacebook = function() { var auth = firebase.auth(); var provider = new firebase.auth.FacebookAuthProvide ...

Having trouble with CSS and javascript files not resolving after moving app to a new Windows 7 development machine

I have recently migrated my ASP.Net 3.5 web site from my old XP sp3 machine to a new Win 7 64-bit dev machine. The web application utilizes Master Pages, App_Themes with style sheets and images, as well as an image folder located off the main root. Additio ...

Choose class based on the retrieved information

Good morning, I am working on dynamically setting the class of a td element based on data fetched from a database to reflect any changes or updates. One of the fields returned has four possible options, and I need to modify the CSS class of that field acc ...

Using Angular to display exclusively the selected items from a list of checkboxes

Is there a way to display only the checked items in a checkbox list? I am looking for a functionality where I can select multiple items from a checkbox list and then have an option to show only the selected items when needed, toggling between showing just ...

How can the checkers code be corrected due to a mistake?

Designed a simple game where the objective is to clear all the pieces by jumping over the checkers. However, encountering an error when attempting to remove the checker for the second time. Uncaught TypeError: Cannot read property 'theRow' of u ...

Managing active dropdown menus in VueJS

I'm having trouble figuring out why my navigation menu and method to open subitems on click are not working correctly. [![dropdown_menu][1]][1] new Vue({ el: '#app', data: { //menu "menu_title": "a", "child_ro ...

Creating PHP scripts that can securely handle cross-domain variables within an iframe

I am managing customer data on my server A. My customers, who have their own server B (over which I have no control), embed an IFRAME on their webpage (referred to as the page of origin) that calls a page on my server A to display some customer informatio ...

Click anywhere on the body to conceal this element

I am currently working on a website where I have implemented a hidden menu that is functioning correctly. However, I want to make it so that when the menu is visible, users can click anywhere on the body to hide it. Unfortunately, my current code is not w ...

Utilize Vue.js to take screenshots on your device

After following the tutorial at https://www.digitalocean.com/community/tutorials/vuejs-screenshot-ui, I was able to successfully capture a screenshot with Vue.js. However, it seems that the dimensions of the screenshot are not quite right. Issue: The cap ...

Accessing data attributes using AngularJS

Trying to extract the data attribute from the following code: <button ng-click="EditPlayer(name, position, number, age)" id="btnEdit" class="btn btn-successes" data-playerid="{{player.id}}">Save</button> Within my angular controller: $scope. ...

Tips for incorporating a tooltip into a disabled selectpicker option

A selectpicker I have is filled with various languages, listed as `li` tags within a `ul` tag. You can see an example in the image provided below. https://i.stack.imgur.com/dsg66.png It's noticeable that the `+ Add Language` option in the selectpick ...

Data formatted in JSON within a document

Recently, I came across a code snippet that demonstrated how to extract data from a JSON array: <div id="placeholder"></div> <script> var data={"users":[ { "firstName":"Ray", "lastName":"Villalobos", "joined": ...

"The CURL request is functioning properly, whereas the Ajax request is not yielding the

I currently have a Scrapyd server up and running and I am attempting to schedule a job. When I use the following CURL command, everything works perfectly: curl http://XXXXX:6800/schedule.json -d project=stackoverflow -d spider=careers.stackoverflow.com - ...

Implementing validation for multiple email addresses in JavaScript: A step-by-step guide

Currently, I am utilizing Javascript to perform validation on my webpage. Specifically, I have successfully implemented email validation according to standard email format rules. However, I am now seeking assistance in enhancing the validation to allow for ...

Push information into MongoDB without the need to make changes to the entire entry within the MEAN stack

I have a MEAN Stack single-page application up and running for managing exams. The schema/model I have for an exam (or Klausur in German) looks like this: var KlausurSchema = new Schema( { name: String, semester: String, krankm ...