Calling a method in an angular Component from an external JS file

Just_like_you

I am developing an e-learning application that plays SCORM content. I'm fairly new to angular and scorm. I'm using angular 4 and spring boot. I am facing difficulty in exposing the backend API to the SCORM content.

These are what I have tried so far:

The below code view-course-content.ts has all the 8 SCORM methods that the SCO will invoke from a javascript file inorder to communicate with the LMS.

view-course-content.component.ts

import { Component, OnInit, HostListener,ElementRef } from '@angular/core';
import {Router, ActivatedRoute, Params} from '@angular/router';
import { LaunchService } from '../services/launch-service.service';
import { DataService } from '../services/data.service';
import { DomSanitizer } from '@angular/platform-browser';
import { ApiService } from '../services/api.service';
import { ViewChild } from '@angular/core';
//import { IndexComponent } from './../index/index.component'




@Component({
  selector: 'app-view-course-content',
  templateUrl: './view-course-content.component.html',
  styleUrls: ['./view-course-content.component.css']
})
export class ViewCourseContentComponent implements OnInit {

  pdfUrl;
  courseId;
  API;
  formData = new FormData();
  pdfSrc;
  api = 'http://localhost:8080';



  @ViewChild('API_1484_11') API_NN;
  @ViewChild('API') API_IE;
   @ViewChild('APIHOLDER') APIHOLDER;

  debug = true;




 constructor(private activatedRoute: ActivatedRoute, private dataService: DataService, private launchService: LaunchService, private _sanitizer: DomSanitizer, private apiService : ApiService,private elementRef: ElementRef) {
   console.log('Unit Id in viewCourse', this.dataService.unitId);  
  this.activatedRoute.params.subscribe((params: Params) => {
       this.courseId = params['id'];
      this.formData.append('courseId', params['id']);
      console.log(this.courseId);
    });
   }

  ngOnInit() {
    this.scormAPI = this._sanitizer.bypassSecurityTrustResourceUrl('assets/js/api.js');


  }


//------------------------------------------
//SCORM RTE Functions - Initialization
//------------------------------------------
Initialize(dummyString) { 
  console.log("Inside Initialize() function");
if (this.debug) { alert('*** LMSInitialize ***'); }
console.log("Inside Initialize() function");
//return this.API_1484_11.Initialize();
// return "true";

this.apiService.initialize().subscribe((response) => {
      console.log('Inside apiService Initialize call in view-course-component-ts ');
     // console.log("Response inside view course content", response['_body']);
})

}

//------------------------------------------
//SCORM RTE Functions - Getting and Setting Values
//------------------------------------------
GetValue(varname) {
if (this.debug) { 
 alert('*** LMSGetValue varname='+varname
                       +' varvalue=value ***');
}


}

LMSSetValue(varname,varvalue) {
if (this.debug) { 
 alert('*** LMSSetValue varname='+varname
                       +' varvalue='+varvalue+' ***');
}
}

 LMSCommit(dummyString) {
if (this.debug) { alert('*** LMSCommit ***'); }
return "true";
}

//------------------------------------------
//SCORM RTE Functions - Closing The Session
//------------------------------------------
LMSFinish(dummyString) {
if (this.debug) { alert('*** LMSFinish ***'); }
return "true";
}

//------------------------------------------
//SCORM RTE Functions - Error Handling
//------------------------------------------
LMSGetLastError() {
if (this.debug) { alert('*** LMSGetLastError ***'); }
return 0;
}

LMSGetDiagnostic(errorCode) {
if (this.debug) { 
 alert('*** LMSGetDiagnostic errorCode='+errorCode+' ***');
}
return "diagnostic string";
}

LMSGetErrorString(errorCode) {
if (this.debug) { 
 alert('*** LMSGetErrorString errorCode='+errorCode+' ***'); 
}
return "error string";
}

}

The below code actually launches the SCORM content. I have put it in a frameset.

view-course-content.component.html

<script type="text/javascript"
  src="assets/runtime/BrowserDetect.js"></script>
    <!-- <script type="text/javascript"
    src="assets/js/SCORM_API_wrapper.js"></script>   -->
     <script type="text/javascript"
    src="assets/js/scormAPI.js"></script>
<div>  


   <div align="center"> View Course Content Page </div> 

     <div id="scormContent" *ngIf = "unitId == 2">

         <frameset frameborder="0" border="0" rows="0,*" cols="*" >


    <frame 
        src="assets/CourseImports/shared/launchpage.html"
        name="Content" id="Content" marginwidth="1px;" marginheight="1px;" noresize="noresize"></frame>



    </frameset>

The courses are launched from the dashboard.

dashboard.component.html

<div class="container-fluid dashboard">
    <div class="row ">


        <h1 class="page-header page-heading animated fadeInDownBig">
            Courses<small></small>
        </h1>



        <div class="col-md-3 col-sm-6" *ngFor='let course of courses'>

            <div class="widget widget-stats bg-aqua-lighter">
                <div class="stats-icon">
                    <i class="fa fa-desktop:before"></i>
                </div>
                <a target="_blank" href="javascript:void(0);"
                    (click)="viewCourse(course.courseId)">

                    <div class="img-hover">
                        <img src="api/admin/getCourseCoverImage{{course.courseId}}"
                            id="coverImage" alt="HTML5 Image" height="70px" width="120px"
                            class="img-responsive img-rounded" />
                    </div>
                </a>

                <div class="stats-info">
                    <!-- <p id="testId_${publishedTestSurvey.testDetails.testId}">${publishedTestSurvey.testDetails.testName}</p>
                                        <h4>${fn:length(publishedTestSurvey.testDetails.testQuestionDetailsSet)}
                                            questions | ${publishedTestSurvey.testDetails.duration}
                                            Minutes | ${publishedTestSurvey.testDetails.testRepetitions}
                                            Total Attempts</h4> -->
                    <p>{{course.courseName}}</p>
                    <div class="stats-link">
                        <!-- <a target="_blank" href="view-course/{{course.courseId}}" (click)="viewCourse(course.courseId)">  
                                                Open Course <i class="fa fa-arrow-circle-o-right"></i>
                                            </a> -->
                        <!-- <a target="_blank" href="javascript:void(0);" (click)="viewCourse(course.courseId)">  
                                                Open Course <i class="fa fa-arrow-circle-o-right"></i>
                                            </a> -->
                    </div>
                </div>
            </div>
        </div>

    </div>    
</div>

These are the calls that the SCO will make to communicate with the LMS

API.Initialize()
API.SetValue()
API.GetValue()
API.Terminate()
API.GetLastError()
API.GetErrorString()
API.GetDiagnostic()

I haven't been able to figure out how to expose the SCORM methods in view-course-content.ts to the above js calls that the SCORM content will be making.

view-course-content.ts is my Javascript API that needs to be called. How can I capture the calls made by the js file and redirect it to the methods in view-course-content.component.ts? I have no control whatsoever on the js file that is making these calls.

Any help will be highly appreciated! Thanks.

Just_like_you

I was able to expose the API in the view-course-content.component.ts file in the ngOnInit function like below:

window["API_1484_11"] = this.api_1484_11

where api_1484_11 is another angular component that had all the 8 API calls implemented and injected into the view-course-content.ts file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Invoke method in external JS file from Vue component

From Dev

call angular component method from external window

From Dev

Calling an angular component method from classic HTML

From Javascript

Angular 2: import external js file into component

From Dev

Using external js file in angular 6 component

From Dev

How to call a method from a JS file into an Angular component

From Dev

Angular 14 calling Child Component method from Parent Component

From Dev

Calling a Javascript function from an external js file in the html file

From Dev

Vue.js calling an async function from external js file

From Dev

Calling method from compiled js file

From Dev

Angular 4 - Calling a parent method from a child component not working

From Dev

Angular - Calling method on service from component returns undefined

From Dev

Calling Routes from a js angular 1.5.8 file

From Dev

Call a function of external js file in angular 5 component

From Dev

Vue.js - Calling a component's method from Vue instance

From

Dynamically load external javascript file from Angular component

From Dev

Calling an function that is on an Ionic Angular page from a component in another file

From Dev

Passing data from external js file to vue component

From Dev

Calling a method on Parent Component from Child Component

From Dev

Angular 6 - call Angular function from external js file

From Dev

calling method from one controller to another controller in angular js

From Dev

How can I add external JS file in Angular CLI component, specific to the component only?

From Dev

Calling external Javascript from Action Method

From Dev

Reading from file then excetuing Js code by calling specific method

From Dev

Calling method of a component Angular 2/4

From Dev

Angular 1.5: calling a method of a component inside ngRepeat

From Dev

Filter module from external html file on Angular JS

From Dev

Calling a method from a helper file

From Dev

Calling a function from an external file in Python

Related Related

  1. 1

    Invoke method in external JS file from Vue component

  2. 2

    call angular component method from external window

  3. 3

    Calling an angular component method from classic HTML

  4. 4

    Angular 2: import external js file into component

  5. 5

    Using external js file in angular 6 component

  6. 6

    How to call a method from a JS file into an Angular component

  7. 7

    Angular 14 calling Child Component method from Parent Component

  8. 8

    Calling a Javascript function from an external js file in the html file

  9. 9

    Vue.js calling an async function from external js file

  10. 10

    Calling method from compiled js file

  11. 11

    Angular 4 - Calling a parent method from a child component not working

  12. 12

    Angular - Calling method on service from component returns undefined

  13. 13

    Calling Routes from a js angular 1.5.8 file

  14. 14

    Call a function of external js file in angular 5 component

  15. 15

    Vue.js - Calling a component's method from Vue instance

  16. 16

    Dynamically load external javascript file from Angular component

  17. 17

    Calling an function that is on an Ionic Angular page from a component in another file

  18. 18

    Passing data from external js file to vue component

  19. 19

    Calling a method on Parent Component from Child Component

  20. 20

    Angular 6 - call Angular function from external js file

  21. 21

    calling method from one controller to another controller in angular js

  22. 22

    How can I add external JS file in Angular CLI component, specific to the component only?

  23. 23

    Calling external Javascript from Action Method

  24. 24

    Reading from file then excetuing Js code by calling specific method

  25. 25

    Calling method of a component Angular 2/4

  26. 26

    Angular 1.5: calling a method of a component inside ngRepeat

  27. 27

    Filter module from external html file on Angular JS

  28. 28

    Calling a method from a helper file

  29. 29

    Calling a function from an external file in Python

HotTag

Archive