简体   繁体   English

从JavaScript代码调用Typescript函数

[英]Call typescript function from javascript code

Due to limitations of IE10 and below, I have to recreate the Import/Upload functionality that I created since FileReader is not supported. 由于IE10及以下版本的限制,由于不支持FileReader ,因此我必须重新创建我创建的导入/上传功能。 I decided to use iFrame to meet the requirement to be able to upload using old browser. 我决定使用iFrame来满足能够使用旧浏览器上传的要求。

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>

My javascript. 我的JavaScript。 I didn't finish the javascript code snippet because I realized that I had to do bigger rework if I call the action directly. 我没有完成javascript代码段,因为我意识到如果直接调用该动作,则必须做更大的返工。

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

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

See code below that the action in my controller is being called by typescript. 请参阅下面的代码,我的控制器中的动作正在由打字稿调用。 Previously, I can just use ng-click then call this method. 以前,我只能使用ng-click然后调用此方法。

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

I need this typescript function (which is my viewmodel as angular) to be called using javascript and unfortunately I have no idea how. 我需要使用JavaScript来调用此打字稿功能(这是我的viewmodel的角度),不幸的是我不知道如何。

The concept is to be able to upload a file via iFrame then on Upload is to call the Typescript function. 这个概念是能够通过iFrame上传文件,然后在Upload上调用Typescript函数。

Typescript is compiled into javascript so this can be done as you would call a javascript function. Typescript被编译为javascript,因此可以像调用javascript函数一样完成此操作。 Having said that, the structure can change a little from what you would expect, the best way I learnt how this worked was to compare the Typescript file to the javascript file that was compiled from it. 话虽如此,结构可能会与您期望的有所不同,我了解到的最佳方式是将Typescript文件与从中编译的javascript文件进行比较。 If you have modules/classes in typescript they will become part of the path to call the function. 如果您在打字稿中有模块/类,它们将成为调用函数的路径的一部分。 Example: 例:

class Foo{
    constructor(){}

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

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

To call this in typescript or javascript you would do (provided you have imported the page correctly). 要使用Typescript或javascript调用此代码,您可以这样做(前提是您已正确导入页面)。

FooModule.FooInstance.Bar();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM