如何在Angular中进行JavaScript库依赖注入? [英] How to do JavaScript Library Dependency Injection in Angular?

查看:33
本文介绍了如何在Angular中进行JavaScript库依赖注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Angular在控制器的基础上包含JavaScript库?

How can I include JavaScript libraries on a controller basis using Angular?

例如,我在该项目中使用了 morris.js ckeditor ,尽管它们仅用于两个数据库,但它们需要近1MB(开发版本)的资源页面.

For example, I'm using morris.js and ckeditor in this project which require nearly 1MB (dev versions) of resources even though they are only used on two pages.

通过在 index.html 文件中引用它们,将它们包含在整个单页应用程序中显然是浪费的.

It's obviously a waste to include these within the entire single page application by referencing them in the index.html file.

有没有一种方法可以使用Angular Dependency Injection将这些包含在控制器中?我正在猜测使用服务或指令,但不确定在这种情况下这些组件的外观.

Is there a way to use Angular Dependency Injection to include these in a controller? I'm guessing using a service or a directive but I'm not sure how those components would look in this case.

推荐答案

最好使用require.js根据需要在SPA中加载这些模块.

You are better off using require.js to load these modules in your SPA based on when you need them.

这篇关于如何在Angular中进行JavaScript库依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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