如何使用旧版 javascript 库作为 Angular 服务的范围依赖项? [英] How do I use a legacy javascript library as a scoped dependency of an Angular service?
问题描述
具体来说,我正在使用一个约 800 行的 SCORM API 包装器库,以促进与 LMS 的通信.作者不是用Angular写的.一个传统的、原始的 js 文件 (index.js) 被包裹在它上面,我包含了两者的片段,以了解这里使用的结构.
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};
最重要的是在全局范围内执行的旧版 javascript 文件.
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;
}
我正在用 Angular 编写一个全新的应用程序,但我想在我的架构中利用一些现有的外部库.我真的很想将 index.js 的功能公开为 Angular 服务,而不必完全重写它.
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.
如果我给你上面提到的两个 javascript 文件和一个具有以下结构的新 services.js 文件
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(){
});
}
]);
如何将 index.js 中的get"和set"函数注入学习者服务的 $scope 中?
how would you inject the "get" and "set" functions from index.js into the $scope of the learner-service?
推荐答案
希望我正确解释了您的问题...
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');
});
]);
这篇关于如何使用旧版 javascript 库作为 Angular 服务的范围依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!