我如何使用传统JavaScript库作为一个角服务的范围依赖? [英] How do I use a legacy javascript library as a scoped dependency of an Angular service?

查看:132
本文介绍了我如何使用传统JavaScript库作为一个角服务的范围依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,我与促进与LMS通信的〜800线SCORM API包装库工作。笔者并没有角度写。遗产,香草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;
}

我写在角绿地的应用程序,但我想利用一些现有的外部库在我的建筑。我真的想揭露index.js'功能的角度服务,而不必完全重写它。

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功能为学习者服务的范围$?

how would you inject the "get" and "set" functions from index.js into the $scope of the learner-service?

推荐答案

希望我间preTED你的问题正确...

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库作为一个角服务的范围依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆