Sarasota Sun Sarasota Sun - 1 year ago 248
AngularJS Question

Dynamic ui-router with ocLazyLoad using multiple modules in resolve


Working from this question/solution Stackoverflow question .....

I edited the original question I posted here because it is no longer worth reading.

My problem was I could not find documentation on:

  1. Using dynamic angular-ui-router;

  2. That makes use of ocLazyLoad;

  3. Had nested views the main controller did not need to know anything about;

  4. And it facilitates the pursuit of building a REAL WORLD large-scale angular application comprising of a myriad of modules, many of which are simultaneously required in just one view, and again, often without the main controller of that view never needing to know anything about those nested views or how they work;

  5. And! As a bonus, allow me to add those modules used by many states in some base state, so I would never need to load them again (this does not apply to services).

As example:

What if I had a page, called MyDashboard, that may contain typical info on something that MyDashboard would contain.

But what if I wanted to show graphs on that page, and I did not want MyDashboard controller to know about those graphs?

What if I had a To-Do list that I used throughout my app and did not want MyDashboard controller to know anything about those to-dos?

Is there a way I can add dependencies for nested views that MyDashboard controller need not know how they work, WHILE AT THE SAME TIME HAVE DYNAMIC STATE?

Is there a way I could, while creating ---DYNAMIC--- state views, just supplant those dependencies I needed for that view, often w/o the controller never needing to know anything about them?

I think the below is the best solution to creating a large scale REAL WORLD, agnostic, angular app, with dependencies loaded on a "need-to-have" basis.

I scoured ocombe's github for such a solution. You won't find it.

I offer the below 'Ronco-Automatic' solution to the community. It slices, it dices, it peels. I hope it helps you.

Post Script: This is not your father's 'Hello World' sappy example.

Answer Source

You WILL NOT find this in the documentation:

define(['app'], function (app) {
 'use strict'; ($q, $rootScope, $state, $window, MenuSvc) {
     // Typical call to my factory, of course does not have to be json
     MenuSvc.all().success(function (states) {
         angular.forEach(states, function (state) {
             try {
                 // This is where the magic occurs, so simple, but so hard
                 // to find documentation. :(
                 var result = [];
                 angular.forEach(state.dependencies, function (dependency) {
                         "name" : dependency.module,
                         "files": dependency.files,
                        // this means to load the dependencies in order & not parellel
                        // especially important when you are loading css files dynamically
                         "serie" : true,
                         // cache in memory please
                         "cache" : true
                     // state.resolve is a function call of state ($stateProvider)
                     // [state.resolve] is the property 'resolve' of object 'state' in my json
                     state.resolve[state.resolve] = function ($ocLazyLoad) {
                         return $ocLazyLoad.load(result)
             } catch (e) {
                 console.log('Error: ' + e.message);
             //$stateProviderRef is a global variable I declared in app.config, eg:
             //$urlRouterProviderRef = $urlRouterProvider;
             //$stateProviderRef = $stateProvider;
             //Remember: providers must be instantiated in config
             $stateProviderRef.state(, state);
         // You must designate default state in a dynamic ui-router in the Must!!
         // but this does not have to be hard-coded. You can do an if statement on a property
         // called, eg 'startUp" above and pass that state name below as a variable
         // eg. $state.go(startUp) which is designated above in the for each
 }) });

My Json format:

[   {
"name": "app",
"abstract": true,
"url": "",
"views": {
  "root": {
    "templateUrl": "app/layout/views/tpl.layout.html",
    "controller": "LayoutCtrl as layout",
    "requiresAuthorization": true
  "base@app": {
                "templateUrl": "app/layout/views/partials/tpl.base.html"
              // You can go wild and add controllers here and add them to
              // your dependencies below. Eg, My base has own modules different from root.
  "header@app": {"templateUrl": "app/layout/views/partials/tpl.header.html"},
  "nav@app": {"templateUrl": "app/layout/views/partials/tpl.navigation.html"},
  "ribbon@app": {"templateUrl": "app/layout/views/partials/tpl.ribbon.html"}
"resolve": {},
"dependencies": [
    "module": "app.Layout",
    "files": [
      // Don't put comments like this in your json, but I discovered that you
      // can list controllers/directives once in a base module and all children modules will
      // inherit. HOWEVER, that isn't the case for svcs; they must be loaded where/when needed
      (all the needed controllers and directives for layout),
      (all the css files needed for layout)
    "module": "app.Graphs",
    "files": [
      //Don't put comments like this in your json, but I loaded these graphs
      //in layout as they will be used in various modules, so don't need to load again in app
]   },   {
"name": "app.MyDashboard",
"views": {
  "content@app": {
    "templateUrl": "app/dashboards/_mydashboard/myDb/views/",
    "controller": "MyDashboardCtrl as myDb",
    "requiresAuthorization": true
"resolve": {},
"dependencies": [
    "module": "app.MyDashboard",
    "files": ["app/dashboards/_mydashboard/myDb/controllers/MyDashboardCtrl.js"]
    "module": "app.ToDo",
    "files": [
      // This svc needs to be loaded every where it is needed
]   } ]

It works !!!!!!!!!!!

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download