简体   繁体   English

在Angularjs中加载外部模块(第三方库)

[英]Load external modules (third party libs) in Angularjs

Until recently I used to develop my js applications with Backbone-js. 直到最近,我还使用Backbone-js开发我的js应用程序。

Now, I wish to start using Angular-js for the first time. 现在,我希望第一次开始使用Angular-js。

In my backbone-js apps I used requirejs to load any third party library, it let my app stay organized and clean. 在我的骨干js应用程序中,我使用requirejs加载了任何第三方库,这使我的应用程序保持井井有条和干净。

Now, when playing around with angular, I see that in many examples they use <script> to load these modules. 现在,在使用angular时,我看到在许多示例中,它们使用<script>加载这些模块。

Is there a clean way to load modules without using this <script> tag? 有没有使用此<script>标记加载模块的干净方法? and keep angular functioning as expected? 并保持角度功能正常? Is it common to use require-js for angularjs apps? 对angularjs应用程序使用require-js是否常见? or is there any alternative? 还是有其他选择?

Thanks. 谢谢。

Angular uses whats called dependency injection to handle modules. Angular使用所谓的依赖注入来处理模块。 Here is the documentation: https://docs.angularjs.org/guide/module 这里是文档: https : //docs.angularjs.org/guide/module

You don't need to use require since you have a built in dependency injection mechanism. 由于您具有内置的依赖项注入机制,因此不需要使用require。 In order to use 3rd party libraries you need to do 3 things: 为了使用第三方库,您需要做以下三件事:

  1. Use libraries that are compatible with angular in order be synced with the digest cycle. 使用与angular兼容的库,以便与摘要周期同步。 Most of the common libraries have an angular module that encapsulates their code. 大多数通用库都有一个用于封装其代码的angular模块。

  2. Add these modules to your app. 将这些模块添加到您的应用程序。 You can explicitly add each library with its own script tag or you can create a bundle of all your libraries and include only it. 您可以使用自己的脚本标签显式添加每个库,也可以创建所有库的捆绑包并仅包含它。

  3. Declare the use of that module when you create your app and module. 在创建应用和模块时,声明该模块的使用。

You might want to look at https://github.com/substack/node-browserify#usage 您可能想看看https://github.com/substack/node-browserify#usage

The downside is you'll introduce a "compile phase" to your build process. 缺点是您将在构建过程中引入“编译阶段”。

The plus is the npm integration. 加号是npm集成。

You still need to follow the "angular way" to inject dependencies, using browserify you'll have little yet nice puses https://blog.codecentric.de/en/2014/08/angularjs-browserify/ 您仍然需要遵循“角度方式”来注入依赖关系,使用browserify,您将获得很少但不错的使用方法https://blog.codecentric.de/zh/2014/08/angularjs-browserify/

Hope it helps. 希望能帮助到你。

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

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