abhit
abhit

Reputation: 979

How to load controllers in ionic only when they are needed?

I have a controllers folders in www/js/controllers where I keep all the controllers needed by my app. But when I start the app all the controllers are loaded at once. Now how can I load only those controllers whose view is being used and keep other controllers at peace.

Upvotes: 0

Views: 270

Answers (2)

Manikandan Velayutham
Manikandan Velayutham

Reputation: 2228

I don't know what you are trying to ask exactly.

See, JavaScript load All files(even won't take much time) while execute the app. Note it, files load only(allocate the memory) not execute the function except on-fly function. Ex (Angularjs, Jquery, Underscore - the file load first time only but function execute when we use).

Angularjs is JS framework right?. So same like above, here we are telling to execute controller in routing or ng-controler directive as per view, Note the file was loaded before only. In single view we can execute many controllers(ng-controller) as per need.

Create Controllers & Services in IIFE based. It's good.

Upvotes: 0

Ved
Ved

Reputation: 12103

To dynamically load controllers, you need to use RequireJS.

http://requirejs.org/

  1. Using https://oclazyload.readme.io/docs/with-your-router

Or, Try this:

If you are using ngRoute:

   $routeProvider
        .when('/url',
            {
                templateUrl: '/views/abc.html',
                resolve: resolveController('/controllers/abc.js')
            });

Using UI-Router:

.state('abc',{
            url : '/abc',
            templateUrl : 'views/abc.html',
            resolve: resolveController('/controllers/abc.js')
        })

Upvotes: 1

Related Questions