问题
Specifically, I am working with an ~800 line SCORM API wrapper library that facilitates communication with an LMS. The author did not write it in Angular. A legacy, vanilla js file(index.js) has been wrapped over top of it I'm including a snippet of both to give an idea of the structure being used here.
SCORM = { //Define the SCORM object
version: null, //Store SCORM version.
handleCompletionStatus: true, //Whether or not the wrapper should automatically handle the initial completion status
handleExitMode: true, //Whether or not the wrapper should automatically handle the exit mode
API: { handle: null,
isFound: false } //Create API child object
};
SCORM.API.get = function(){ //implementation};
SCORM.API.set = function(){ //implementation};
On top of this is a legacy javascript file that executes in global scope.
index.js
var scorm = SCORM;
var interval;
var channel;
function init(){
scorm.version
}
function get(params){
var values;
values = getFromLMS(params);
return values;
}
function set(param, value){
return scorm.set(param, value);
return callSucceeded;
}
I am writing a greenfield app in Angular, but I would like to leverage some existing external libraries in my architecture. I would really like to expose index.js' functionality as an Angular service without having to completely rewrite it.
If I were to give you the two javascript files mentioned above and a new services.js file that had the following structure
'use strict';
var learnerServices = angular.module('learnerServices', []);
learnerServices.factory('learner-service' [
function(){
});
}
]);
how would you inject the "get" and "set" functions from index.js into the $scope of the learner-service?
回答1:
Hopefully I interpreted your question correctly...
'use strict';
var learnerServices = angular.module('learnerServices', []);
learnerServices.factory('scormService' [
function() {
var scorm = SCORM;
var interval;
var channel;
function init(){
scorm.version
}
function get(params){
var values;
values = getFromLMS(params);
return values;
}
function set(param, value){
return scorm.set(param, value);
return callSucceeded;
}
return {
scorm: scorm,
interval: interval,
channel: channel,
init: init,
get: get,
set: set
}
});
]);
learnerServices.factory('learnerService' ['scormService'
function(scormService){
scormService.get('some param');
});
]);
来源:https://stackoverflow.com/questions/24936511/how-do-i-use-a-legacy-javascript-library-as-a-scoped-dependency-of-an-angular-se