简体   繁体   English

Requirejs:仅在需要时加载依赖项

[英]Requirejs: Load Dependancies only when needed

The dependancies defined in define block of app.js get loaded. 将加载app.js的define块中定义的依赖项。

What I want to do is: 我想做的是:

If I am going to load Dashboard controller it requires charts related dependancy,and any other controller never needs this dependancy. 如果要加载Dashboard控制器,则需要与图表相关的依赖关系,而其他任何控制器都不需要此依赖关系。

Problem: When App loads, all dependancies getting loaded, that I don't want. 问题:加载应用程序时,所有不需要的依赖项都会被加载,这是我所不希望的。

Note: Dependancies loaded means js files loaded. 注意:加载的依赖项意味着加载了js文件。

main.js main.js

require.config({ 
baseUrl: "http://localhost/ums/angular/js",
    paths: {
        'angular': 'lib/angular.min',
        'ngRoute': 'lib/angular-route.min',
        'flash': 'lib/angular-flash',
        'angular-loading-bar': 'lib/loading-bar.min',
        'ngAnimate': 'lib/angular-animate.min',
        'ui.bootstrap': 'lib/ui-bootstrap-tpls-0.12.0',
        'uniqueField': 'admin/directives/angular-unique',
        'input_match': 'admin/directives/angular-input-match',
        'uniqueEdit': 'admin/directives/angular-unique-edit',
        'angularAMD': 'lib/angularAMD.min',
        'googlechart':'lib/ng-google-chart',     
        'app': 'admin/app',
    },
    waitSeconds: 0,
     shim: { 
     'angular': { exports: 'angular'},
    'angularAMD': { deps: ['angular']},
     'googlechart': { deps: ['angular']},
    'ngRoute':{ deps: ['angular']},
    'angular-loading-bar':{ deps:['angular'] },
    'ngAnimate': { deps:['angular'] } ,
    'ui.bootstrap': {deps: ['angular'] },
    'uniqueField': {deps: ['angular'] },
    'input_match': {deps: ['angular'] },
    'uniqueEdit': {deps: ['angular'] },
    'flash': {deps: ['angular'] },
    },
    deps: ['app']
});

app.js app.js

var base_url="http://localhost/ums/";
define(['angularAMD', 'ngRoute','flash','angular-loading-bar','ngAnimate','ui.bootstrap','uniqueField','input_match',  'googlechart'  ], function (angularAMD) {
var app = angular.module('angularapp', ['ngRoute','flash','angular-loading-bar','ngAnimate','ui.bootstrap','uniqueField','input_match' ,  'googlechart'  ]);  
app.config(['$routeProvider', function($routeProvider){
/* *************** routes *************** */
.....

Example controller: 控制器示例:

define(['app'], function (app) {
    app.controller('dashboardCtrl', ['$scope','$http', function($scope,$http){
    //I am able to use any of the dependancy here, (I don't need that all dependancies here )
    //only need charts

        }]); 

 });

In your case, all the dependencies are getting loaded at the time of module creation. 在您的情况下,所有依赖项都在模块创建时加载。 If you want to load it at the time of controller, keep it in the controller and remove from the module definition. 如果要在控制器时加载它,请将其保留在控制器中并从模块定义中删除。 See example below 见下面的例子

In your controller 在您的控制器中

define(['app','googlechart'], function (app) {//charts dependencies
  app.controller('dashboardCtrl', ['$scope','$http', function($scope,$http){
   //I am able to use any of the dependancy here, (I don't need that all dependancies here )
  //only need charts

  }]); 

 });

Remove it from the module definition app.js 从模块定义app.js中将其删除

 var base_url="http://localhost/ums/";
//remove it from the module definition
define(['angularAMD', 'ngRoute','flash','angular-loading-bar','ngAnimate','ui.bootstrap','uniqueField','input_match' ], function (angularAMD) {
var app = angular.module('angularapp', ['ngRoute','flash','angular-loading-bar','ngAnimate','ui.bootstrap','uniqueField','input_match' ]);  
app.config(['$routeProvider', function($routeProvider){
/* *************** routes *************** */
.....

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

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