javascript - Lazy loading modules with angularjs and requirejs -


i have been tackling issue long time great difficulty since there lot of articles show how load directives, services, , other angular components use word module describe them.. ending seo nightmare.

all solutions incredibily complex, making dividing js files not relevant.

has found simple way inject angular module dependency main module when it's needed ?

note: since i'm using require.js actual js present (also lazyloaded when it's needed). needed inject modules onto angular.

here how close got:

  1. window function in index.html load secondary .js file

    <script type="text/javascript">     var hasbackloaded = false; function loadbackofficejs (cb) {     console.log("loadbackofficejs");     if(!hasbackloaded){         var newscript = document.createelement('script');         newscript.type = 'text/javascript';         newscript.src = '<%=dist_cdn%>/scripts/back.js';         newscript.onload = backloaded;         document.body.appendchild(newscript);         function backloaded(){             console.log("back loaded")             hasbackloaded = true;             cb();         }      } else {         cb();     } } 

  2. call previous function , try load angular modules angular

    loadbackofficejs(function(){ require(['back'], function(){ //angular modules should injected here.. somehow }); });

you should take @ lib oclazyload

most basic usecase load module provided $oclazyload service.

$oclazyload.load('yourmodule.js'); 

this article written lib's author when started library. though it's bit old it's entry point internals of lazy loading modules.


Comments

Popular posts from this blog

android - net_scheduler holding wakelock -

sql - MySQL : Getting Entries from a many-to-many table -

java - Retrieving data from database using jsp (Hibernate + Spring + Maven) -