简体   繁体   English

如何在角度中读取模块与vanilla JS库?

[英]How to read module vs. vanilla JS library in angular?

I'm new to service/provider/factories modules in angular. 我是角度服务/提供商/工厂模块的新手。 I've been using a library called annyang for 30 mins and I wanted to find the more "angular" way of using this library. 我一直在使用一个名为annyang的库30分钟,我想找到使用这个库的更“有棱角”的方式。

Some independent on git made a moduleProvider found here: ngAnnyang . 一些独立于git的人在这里找到了一个moduleProvider: ngAnnyang

It has zero documentation on how to use it properly. 它没有关于如何正确使用它的文档。

But traditional annyang you simply run: 但传统的annyang你只需运行:

var commands = {
  'remind me to *val' : reminders,
};

annyang.addCommands(commands);
annyang.start();

QUESTIONS: After injecting the module into my angular application: 问题: 将模块注入我的角度应用程序后:

  1. would this module be implemented the same or commands now get prefixed with ng like: ngAnnyang.addCommands(); ngAnnyang.start(); 将此模块实现相同或现在命令的前缀为ngngAnnyang.addCommands(); ngAnnyang.start(); ngAnnyang.addCommands(); ngAnnyang.start(); ?

  2. Do i still need to have the original lib of annyang as a script tag in the app? 我还需要在应用程序中将annyang的原始库作为脚本标记吗?

  3. What in this angular wrapper makes it "angular" except that it now can be injected? 这个角度包装的内容使它“有角度”,除了它现在可以注入?

JS:ng-annyang JS:NG-annyang

    /**
* ngAnnyang Module
*
* Annyang Angular wrapper
*/
angular.module('ngAnnyang', ['ng'])
  .provider('ngAnnyang', function ngAnnyangProvider() {
    'use strict';

    var isEnabledFn = function() {};

    // events
    console.log('ngAnnyang: set events');
    annyang.addCallback('start', function() {
      console.log('ngAnnyang: enabled true');
      isEnabledFn(true);
    });
    _.each(['end', 'error', 'errorNetwork', 'errorPermissionBlocked', 'errorPermissionDenied'], function(v) {
      annyang.addCallback(v, function() {
        console.log('ngAnnyang: enabled false');
        isEnabledFn(false);
      });
    });

    console.log('ngAnnyang: start');
    annyang.start();

    this.debug = function(state) {
      if(state){
        console.log('ngAnnyang: set debug', !!state);
        annyang.debug(!!state);

      } else {
        console.log('ngAnnyang: set debug', true);
        annyang.debug();
      }
    };

    this.setLanguage = function(lang) {
      if(lang){
        console.log('ngAnnyang: debug', ''+lang);
        annyang.setLanguage(''+lang);
      }
    };

    this.$get = function ngAnnyangFactory(){
      return {
        isEnabled: function(fn) {
          console.log('ngAnnyang: isEnabled callback', fn);
          isEnabledFn = fn;
        },
        addCommands: function(commands) {
          console.log('ngAnnyang: add commands', commands);
          annyang.addCommands(commands);
        },
        removeCommands: function(commands) {
          console.log('ngAnnyang: remove commands', commands);
          annyang.removeCommands(commands);
        }
      };
    };
  });

You will need to use the following: 您需要使用以下内容:

angular.module("your app name").config("ngAnnyangProvider", function( ngAnnyangProvider) { angular.module(“你的app name”)。config(“ngAnnyangProvider”,function(ngAnnyangProvider){

ngAnnyangProvider.addCommands (...); ngAnnyangProvider.addCommands(...);

}); });

For more details: 更多细节:

https://docs.angularjs.org/guide/module https://docs.angularjs.org/guide/module

would this module be implemented the same or commands now get prefixed with nglike: ngAnnyang.addCommands(); ngAnnyang.start(); 将此模块实现相同或现在命令以nglike为前缀: ngAnnyang.addCommands(); ngAnnyang.start(); ngAnnyang.addCommands(); ngAnnyang.start(); ?

It seems like that ngWrapper starts the service as soon as the provider is instantiated. 似乎ngWrapper在实例化提供程序后立即启动服务。 You would use it in the same way: 你会以同样的方式使用它:

var app = angular.module('app',[]);

app.controller('SpeechRecognize', ['ngAnnyang','tpsService',function(speechRecognition, tpsService) {
    var commands = {
        'show tps report': function() {
          tpsService.show();
        }
    };
    speechRecognition.addCommands(commands);
}]);

Do i still need to have the original lib of annyang as a script tag in the app? 我还需要在应用程序中将annyang的原始库作为脚本标记吗?

Yes as this is only a wrapper around the original library. 是的,因为这只是原始库的包装。

What in this angular wrapper makes it "angular" except that it now can be injected? 这个角度包装的内容使它“有角度”,除了它现在可以注入?

It seems like that is the only benefit that it provides. 似乎这是它提供的唯一好处。 This also makes it easier to test. 这也使测试更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM