8

I have AngularJs ui router in my application. I need to load js file based on my state.

.state('root.home',{
        url: '/index.html',
        views: {
            'header': {
                templateUrl: 'modules/header/html/header.html',
                controller: 'headerController'
            },          
            'content-area': {
                templateUrl: 'modules/home/html/home.html',
                controller: 'homeController'
            },
            'footer': {
                templateUrl: 'modules/common/html/footer.html',
                controller: 'footerController'
            }
        },
        data: {
            displayName: 'Home',
         }
    })

The headerController,homeController and footerController have different js files when the home state is loading at that time we need to load the controller js files Is it possible through the UI router?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Sarath
  • 1,459
  • 3
  • 22
  • 38
  • Are the js files angular or non angular? – Frozen Crayon Jun 08 '15 at 15:19
  • See http://stackoverflow.com/questions/28184616/angularjs-lazy-load-template-and-controller-in-ui-router-requirejs-oclazyload and http://stackoverflow.com/questions/22627806/angular-ui-router-with-requirejs-lazy-loading-of-controller – Frozen Crayon Jun 09 '15 at 12:19

3 Answers3

0

I think you need to use a framework like RequireJs it is loading your Script files base on the page you are showing to the user

RequireJS loads all code relative to a baseUrl.

check it. I think It might be helpful.

Mehrdad Kamelzadeh
  • 1,764
  • 2
  • 21
  • 39
0

You should not load javascript files on the fly depending on your route status, instead you should concatenate all your js files and minify the compiled source via Gulp for instance.

https://www.npmjs.com/package/gulp-concat https://www.npmjs.com/package/gulp-minify

Nicolas S.
  • 330
  • 1
  • 3
  • 10
-1

ui-route is more powerful than requirejs (ng-route).

You may look her about differences:

What is the difference between angular-route and angular-ui-router?

http://www.amasik.com/angularjs-ngroute-vs-ui-router/

Community
  • 1
  • 1
Lev K.
  • 344
  • 4
  • 4