简体   繁体   English

在Angular模板中包含脚本的最佳实践?

[英]Best practice for including scripts in Angular templates?

I have an Angular SPA with several tabs, which are served by templates. 我有一个带有几个选项卡的Angular SPA,这些选项卡由模板提供。 Each of these tabs requires different scripts (both local and CDN), so I'd only like to load scripts when needed, ie I will need to include them inside the templates (or associated controllers) themselves. 这些选项卡中的每个选项卡都需要不同的脚本(本地和CDN),因此我只想在需要时加载脚本,即,我需要将它们包括在模板(或关联的控制器)本身中。

So far, I've tried this approach: 到目前为止,我已经尝试过这种方法:

// Start template
<data-ng-include src="http://maps.googleapis.com/maps/api/js"></data-ng-include>
// Rest of stuff
// End template

This doesn't seem to be working. 这似乎不起作用。 Yes, I have jQuery included in the header. 是的,我在标题中包含了jQuery。 What is the best way to go about this? 最好的方法是什么? Seems like it should be much simpler to include scripts inside templates. 似乎在模板中包含脚本应该更简单。

You can use any of the lazy loader (on demand loader) library like requireJS, ocLazyLoad . 您可以使用任何惰性加载器(按需加载器)库,例如requireJS, ocLazyLoad

I have successfully implemented ocLazyLoad in one of our enterprise application, because it provide lots of usefull features if you are developing modular Single Page Application: 我已经在我们的一个企业应用程序中成功实现了ocLazyLoad ,因为如果您正在开发模块化的单页应用程序,它可以提供许多有用的功能:

  • Easy to implement. 易于实现。 You can lazy load any resource anywhere inside your application 您可以在应用程序中的任何地方延迟加载任何资源
  • Dependencies are automatically loaded 依赖项会自动加载
  • Debugger friendly (no eval code) 调试器友好(无评估代码)
  • Can load any client side resouce like js/css/json/html 可以加载任何客户端资源,例如js / css / json / html
  • Compatible with AngularJS 1.2.x/1.3.x/1.4.x 与AngularJS 1.2.x / 1.3.x / 1.4.x兼容
  • You can also load any resource inside service, factory, directive also. 您还可以在服务,工厂,指令中加载任何资源。 See example 看例子
  • Resolve any resource before executing any state if you are using ui.router library for routing. 如果使用ui.router库进行路由,请在执行任何状态之前解析所有资源。 See example 看例子
  • You can also lazy load resource in .config() of any module. 您也可以在任何模块的.config()中延迟加载资源。 See example 看例子
  • It also gives you the functionalty of logging module loading events. 它还为您提供了记录模块加载事件的功能。

All references are taken from official website of ocLazyLoad 所有参考均来自ocLazyLoad的官方网站

If you want to include JS. 如果要包括JS。 I would tell you , please use : 我会告诉你,请使用:

cLazyLoad JS cLazyLoad JS

Here is example: 这是示例:

var myapp = angular.module('myApp', ['oc.lazyLoad']);

myApp.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
        .state('home', {
            url: '/home',
            template: '<div ui-view class="ngslide"></div>',
            resolve: {
                deps: ['$ocLazyLoad', function ($ocLazyLoad) {
                        return $ocLazyLoad.load([js/jquery.main.min.js','js/ie10-viewport-bug-workaround.js']);
                    }]
            }
        })
});

I use a simple directive for this (here : 我为此使用了一个简单的指令(在这里:

interface AddScriptDirectiveAttributes extends IAttributes {
  type: string;
  src: string;
}

export function addScript(): IDirective {
  return {
    restrict: 'E',
    link: function (scope: IScope, element: JQuery, attrs: AddScriptDirectiveAttributes) {
      let script = document.createElement('script');
      if (attrs.type) {
        script.type = attrs.type;
      }
      script.src = attrs.src;
      document.body.appendChild(script);
    }
  };
}

Usage would be something like this: 用法将是这样的:

<add-script src="script" type="application/javascript"></add-script>

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

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