Summer Developer Summer Developer - 6 days ago 6
AngularJS Question

Angular UI Router Controller Provider $HTTP

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.

Answer

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

Comments