Executing JavaScript functions within TypeScript

Currently, I am working on integrating the online document viewer into my project and stumbled upon a website called .

As I explored the developer APIs at , I discovered that I could access any document by providing its URL. However, I am using Angular 4 with Typescript so I encountered some challenges.

To tackle this issue, I included the JavaScript code in my index.html file:

index.html
<script type="text/javascript" src="https://api.rollapp.com/1/js/rollmyfile.js"></script>

Next, I needed to utilize this JS file to pass the URL as follows:

<script type="text/javascript">
    var key = "SeCur3AP1K3y";

    var rollMyFile = new RollMyFile(key);
</script>
rollMyFile.openFileByUrl("https://www.example.com/documentation/overview.docx");

Subsequently, I attempted to use .openFileByUrl in my .component.ts file.

I also referenced the path in my .component.ts like this:

///<reference path="https://api.rollapp.com/1/js/rollmyfile.js"/>

However, I am still encountering an issue where I cannot create an instance of RollMyFile.

The error message states:

[ts] Cannot find name 'RollMyFile'
.

Could you please advise on what might be going wrong in this scenario?

Answer №1

To successfully utilize the RollMyFile function in your TS file, it is necessary to declare it at the beginning.

 declare const RollMyFile: any;

ngOnInit() {
 console.log("hi");
 var key = "SeCur3AP1K3y";
 var rollMyFile = new RollMyFile(key);
 console.log('rollMyFile',rollMyFile)
}

An error is occurring in the console due to an incorrect secret key being used.

Answer №2

Give this a shot in your component:

declare RollMyFile: any;
constructor(private _script: ScriptLoaderService) {}
ngOnInit() {
    this._script.load('body', 'https://api.rollapp.com/1/js/rollmyfile.js')
        .then(result => {
        });
    }
}

Then, utilize RollMyFile in your code like this:

(<any>RollMyFile).somfunction();

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

"Enhancing User Experience with Animated Progress Bars in Three.js

Is there a way to create a progress bar animation in three.js? I've been grappling with this issue for quite some time now. I attempted to replicate the html5 video player progress bar method, but unfortunately, it doesn't seem to be compatible w ...

Interactive feature on Google Maps information window allowing navigation to page's functions

Working on an Angular2 / Ionic 2 mobile App, I am utilizing the google maps JS API to display markers on a map. Upon clicking a marker, an information window pops up containing text and a button that triggers a function when clicked. If this function simpl ...

The functionality of json_encode seems to vary when applied to different PHP arrays, as it successfully encodes data for

My current challenge involves importing three arrays from PHP into JS using json_encode. Below is the code snippet I am currently working on: <?php $query2 = "SELECT * FROM all_data"; $result2 = $conn->query($query2); $bu = []; ...

Apple Automation: Extract a targeted string from text and transfer it to a different spot within the page

Apologies for my lack of expertise in this area, but I hope to convey my question clearly. I am working on a form where I need to input text in order to copy specific items and paste them elsewhere on the same webpage. For example, if the input text is " ...

Discover the secret to instantly displaying comments after submission without refreshing the page in VueJS

Is there a way to display the comment instantly after clicking on the submit button, without having to refresh the page? Currently, the comment is saved to the database but only appears after refreshing. I'm looking for a solution or syntax that can h ...

Is there a backend-exclusive solution for scheduling periodic function calls without using JavaScript?

In the JavaScript file that I have included in the main .cshtml page of my ASP.NET MVC application, the following code can be found: var ajax_call = function () { $.ajax({ type: "GET", cache: false, url: "/Session/Index/", ...

Instructions on creating a JSON patch in Django REST Framework

I have a PATCH button in my ModelViewSet https://i.sstatic.net/qoQLu.png class CompanyViewSet(viewsets.ModelViewSet): serializer_class = s.CompanySerializer queryset = m.Company.objects.all() def patch(self, request, id, format=None): ...

What is the method for setting the proxy URL in Webpack.config.js following the deployment of the application?

When running on my local machine, the React front-end is hosted on localhost:3000 while the Node/Express back-end is hosted on localhost:8080. Within the webpack.config.js file for the front-end, I utilize a proxy to enable the front-end to retrieve data ...

Creating stunning 3D animations using Canvas

I am knowledgeable about Canvas 2D context, but I would like to create a 3D animation similar to this example. Would using the Three.js library be the most suitable option for this type of animation? Any recommendations for tutorials or documentation tha ...

Utilizing jQuery functions within Vue components in Quasar Framework

I've recently started delving into web app development and I'm encountering some basic questions regarding jQuery and Vue that I can't seem to find answers to. I have an application built using the Quasar Framework which frequently involves ...

What is the best way to enhance a map by incorporating a variable from Firebase?

I am currently facing an issue while trying to integrate a map into a specific key within my fire-store setup. Upon checking the console, I noticed that I am receiving an undefined error and an empty map is being added to fire-store. My goal is to upload ...

Guide to obtaining context vnode within vue 3 custom directives

Unable to retrieve context from directive <template lang="pug"> div(class="form") select(name="name" v-model="form.input" v-select="form.inputs") option(v-for="(option, ke ...

Display the HTML element prior to initiating the synchronous AJAX request

I'm looking to display a <div> upon clicking submit before triggering $.ajax() Here's my HTML: <div id="waiting_div"></div> This is the CSS: #waiting_div { position: fixed; top: 0px; left: 0px; height: 100%; ...

Loop through XMLHttpRequest requests

I have been attempting to send multiple server requests within a for loop. After coming across this particular question, I tried out the recommended solution. Unfortunately, it doesn't seem to be functioning as expected. for (var i = 1; i <= 1 ...

Unique text: "Custom sorting of JavaScript objects in AngularJS using a special JavaScript order

I'm working with an array structured like this: var myArray = []; var item1 = { start: '08:00', end: '09:30' } var item2 = { start: '10:00', end: '11:30' } var item3 = { start: '12:00& ...

Error 404 encountered when attempting to send a JSON object to the server

I've been facing a problem while trying to send a JSON object to the server using AJAX calls. I keep getting a 404 Bad Request error. The issue seems to be related to the fact that I have a form where I convert the form data into a JSON object, but th ...

What is the best way to navigate to a different page when scrolling in React Router?

I am working on a design that includes a landing page with 100vh and 100vw dimensions. I want it to transition into another page as the user scrolls down. How can I achieve this using React hooks and React Router? Any guidance or suggestions would be high ...

Only include unique objects in the array based on a common property

I am currently working with the following array: [ {name: "Mike", code: "ABC123"}, {name: "Sarah", code: "DEF456"}, {name: "John", code: "GHI789"}, {name: "Jane", code: "JKL01 ...

What is the best way to retain data after clicking a button?

I am facing an issue where I need to figure out how to add information to a new page when a button is clicked. For example, let's say I have an "add to cart" button and upon clicking it, I want to store some data. How can I achieve this functionality? ...

Varied elevations dependent on specific screen dimensions

There seems to be a minor issue with the height of the portfolio container divs at specific window widths. The problematic widths range from 1025 to 1041 and from 768 to 784. To visualize this, try resizing your browser window to these dimensions on the fo ...