加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 在meanjs中注入自定义模块的最佳实践

发布时间:2020-12-17 16:57:26 所属栏目:安全 来源:网络整理
导读:据我所知,没有模块的经典注册,我们可以在其中注入依赖项,如: var myModule = angular.module('myModule',[otherModule]); 但是在directorys的根目录下有文件module-name.client.module.js 'use strict';// Use Applicaion configuration module to register
据我所知,没有模块的经典注册,我们可以在其中注入依赖项,如:

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

但是在directorys的根目录下有文件module-name.client.module.js

'use strict';
// Use Applicaion configuration module to register a new module
ApplicationConfiguration.registerModule('module-name');

我可以在这里注入我的模块,如* .registerModule(‘module-name’,[someModule]);或者我应该在angular.module(‘articles’)中配置.config(…)?

但在配置中我只能注入提供者,没有工厂

解决方法

至于我使用角度,加载自定义模块和外部库的最佳做法
是将angularjs与requirejs结合起来.

它完成了3步.

首先,在主html文件中加载一个构建系统基础的js文件(require.config):
在这里您可以获得所有参数:https://github.com/jrburke/r.js/blob/master/build/example.build.js

示例:
在你的html文件中:

<script src="path/to/require.js" data-main="path/to/yourBuildFile"></script>

在yourBuildFile文件中:

require.config({
    // you can define suitable names for all your external js library
    paths:{
        // don't put a ".js" at the end of the file
        'angular' : 'path/angular','underscore' : 'path/underscore-min','...': '...',},// and other useful things
});

其次,在同一个文件或另一个文件中(参见上面链接中的参数deps),引导您的应用:
像这里解释的那样:https://docs.angularjs.org/guide/bootstrap

例:

// AMD module injection,more info here : http://requirejs.org/docs/whyamd.html
define([ // inject all the external library you need + the definition of your app
    'angular','require','yourpath/yourApp' // don't bother here,it's explained in third point
],function(angular){
    // link your app to the document file programatically
    angular.bootstrap(document,['nameOfYourApp']); 
});

第三,你定义你的应用程序(在“yourpath / yourApp”中)

例:

define([
    'angular',// put all path to your directives + controllers + services
],function(angular){ // only specify parameters the library you use in the function
    // you create your sublime app :)
    angular.module('nameOfYourApp',[
        // put all the name of your modules injected above
    ]);
});

以上示例适用于单页面应用程序.
您可以在此处找到多页应用程序的其他示例
http://requirejs.org/docs/start.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读