0

I was hoping after a user logs in I could load a controller for the logged in view in Angular UI Router as simply as this:

 "content@":{
    templateProvider: function($http, $stateParams) {
        return $http({
        method: 'GET',
        url: '/homeV'
      }).then(function successCallback(html) {
        return html.data;
      });
       },
     controllerProvider: function($http, $stateParams) {
        return $http({
        method: 'GET',
        url: '/homeC'
      }).then(function successCallback(html) {
        return html.data;
      });
  }
       }

But controllers are not templates, and it doesn't appear to work as easily as I had hoped. There are a lot of questions here about similar concerns, but I don't want to have to use Require.js if I don't have to.

Is Require.js the only option? If so I will experiment with that, but I wanted to make sure I'm not missing something first.

Summer Developer
  • 2,056
  • 7
  • 31
  • 68
  • you want to load a js or html after login succes? – Aravind Nov 27 '16 at 14:00
  • @Aravind Yes, because I don't want to front-load the SPA with a bunch of logic only necessary for authenticated users. I want it to be modular and to have the controllers only loaded for that state when that state is entered into. Does that make sense? – Summer Developer Nov 27 '16 at 14:03
  • are you looking an option to load js files dynamically only if the particular modules is in action? – Aravind Nov 27 '16 at 14:12
  • @Aravind how do you mean? I want to load them based on the state the user is in. If this is incorrect let me know. But if I have to use something like require.js I would prefer to do webpack instead. – Summer Developer Nov 27 '16 at 14:17
  • Check out ocLazyLoad: https://oclazyload.readme.io/ – Muli Yulzary Nov 27 '16 at 14:18

1 Answers1

1

As Muli said check out oclazyload. Also, I have my own answer at this post

I have used two modules and injected their respective controllers only when their state is active where I have used resolve option to do that. Some thing like this

resolve: {
            loadMyCtrl: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load('someModule.js');
            }]
         }

The documentation part of oclazyload is not clear. May be you can have a look at my plunker here

Community
  • 1
  • 1
Aravind
  • 40,391
  • 16
  • 91
  • 110
  • My states are nested, and it isn't working. The documentation mentions having two resolves, one for parent and one for child... but I don't understand why if I don't really want to lazy-load in this case so much as load when the child state is entered into. – Summer Developer Nov 27 '16 at 15:10
  • yes dude. I need little more clarification to ur problem ! – Aravind Nov 27 '16 at 15:18