AngularJS需要按需模块(AngularJS require module on-demand)

是否可以加载延迟和包含模块以按需要另一个模块? 像这样的东西:

var app=angular.module('myApp',[]); app.controller('myController',['$scope',function($scope){ $scope.btnClick=function(){ (function(d,s,id){ var js, fjs= d.getElementsByTagName(s)[0]; if(d.getElementById(id)) return; js= d.createElement(s); js.id=id; js.onload=function(){ // do something to make myApp dependent on anotherModule, //like if it were loaded initially: myApp=angular.module('myApp',['anotherModule']); }; js.src='/js/anotherModule.js'; fjs.parentNode.insertBefore(js,fjs); }(document,'script','my_id')); } }]);

我想在$scope.btnClick()上使用来自'anotherModule' $scope.btnClick() 'anotherModule'指令被触发。

Is it possible to load lazy and include module to requires of another module on-demand? Something like that:

var app=angular.module('myApp',[]); app.controller('myController',['$scope',function($scope){ $scope.btnClick=function(){ (function(d,s,id){ var js, fjs= d.getElementsByTagName(s)[0]; if(d.getElementById(id)) return; js= d.createElement(s); js.id=id; js.onload=function(){ // do something to make myApp dependent on anotherModule, //like if it were loaded initially: myApp=angular.module('myApp',['anotherModule']); }; js.src='/js/anotherModule.js'; fjs.parentNode.insertBefore(js,fjs); }(document,'script','my_id')); } }]);

I want to use directive from 'anotherModule' on $scope.btnClick() is triggered.

最满意答案

除非您的模块非常大,我建议不要延迟加载,因为请求很昂贵。 加载一个大文件比许多小文件更快

如果你担心性能,你应该考虑首先通过连接/缩小/ uglification进行优化

这是关于优化您的网站的一个很好的资源: https : //developer.yahoo.com/performance/rules.html

unless your modules are very large, i'd recommend against lazy loading because requests are expensive. it is faster to load one big file than many small files

if you're concerned about performance you should consider first optimizing by concatenation/minification/uglification

heres a good resource about optimizing your site: https://developer.yahoo.com/performance/rules.html

更多推荐