ocombe / ocLazyLoad

Lazy load modules & components in AngularJS
https://oclazyload.readme.io
MIT License
2.63k stars 510 forks source link

What is the use of loadMyCtrl #347

Open sambitp opened 8 years ago

sambitp commented 8 years ago

Hi I am using lazyload durring state route .state('app.home', { url: '/home', templateUrl: 'views/home/home.html', controller: 'homeController', resolve: { // Any property in resolve should return a promise and is executed before the view is loaded loadMyCtrl: ['$ocLazyLoad', function ($ocLazyLoad) { return $ocLazyLoad.load('home'); }] } }) 1.What is the use of loadMyCtrl ?

sambitp commented 8 years ago

Please rectify me if I am wrong I think this is for router dependency like resolve: { loadModule: ["$ocLazyLoad", function($ocLazyLoad) { console.log(1); return $ocLazyLoad.load(['ui.chart']); }], loadMyCtrl: ['$ocLazyLoad', 'loadModule', function($ocLazyLoad, loadModule) { console.log(2); return $ocLazyLoad.load({ files: ['../js/controller/chartController.js'] }); }] }

loadMyCtrl depend upon loadModule

alo commented 7 years ago

Hi!

I don't know if your question is for the router or the ocLazyLoad module.

ui.router You can use resolve to provide your controller with content or data that is custom to the state. resolve is an optional map of dependencies which should be injected into the controller. You can read more here

Your router config

angular.module('myApp').config(
    ['$stateProvider', '$urlRouterProvider',
    function ($stateProvider, $urlRouterProvider) {
      $urlRouterProvider.otherwise('/access/signin');
      $stateProvider
        .state('chart', {
          url: '/chart-view',
          templateUrl: 'path/to/your/view',
          controller: MyController,
          resolve: {
            dep1: function ($http) {
              return $http.get('data.json') );
            }
          }
        });
      }
    ]
  );

Your controller

MyControler.$inject = ['dep1'];
function MyController(dep1){
  conosle.log(dep1.data);
}

You can use this prop with ocLazyLoad too so you can ... Your router config

angular.module('myApp').config(
    ['$stateProvider', '$urlRouterProvider',
    function ($stateProvider, $urlRouterProvider) {
      $urlRouterProvider.otherwise('/access/signin');
      $stateProvider
        .state('chart', {
          url: '/chart-view',
          templateUrl: 'path/to/your/view',
          resolve: {
            foo: function ($ocLazyLoad) { //load external libs 
              return $ocLazyLoad.load(['chart.js', 'otherlib'] );
            },
            myData: function ($http) { //resolve son data 
              return $http.get('data.json'));
            }
          }
        });
      }
    ]
  );

(more)[https://oclazyload.readme.io/docs/with-your-router]