Load controller dynamically in AngularJS

If you are using routing in AngularJS then It’s very important to load controller dynamically. Otherwise, you have to load all controllers at the beginning which is not efficient.

We can load controller dynamically by resolve attribute of $routeProvider
.when function. Please have a look on sample code. Surely, You will understand that.

var app = angular.module ('hw',['ngRoute','ngResource']);
app.config(['$routeProvider', '$controllerProvider', function($routeProvider, $controllerProvider) {
app.registerCtrl = $controllerProvider.register;

function loadcontroller(controllerSubPath){
$.ajaxSetup({async:false});
$.getScript("scripts/controllers/" + controllerSubPath + ".js").done(function( script, textStatus ) {
}).fail(function( jqxhr, settings, exception ) {
console.log( exception );
});
}

$routeProvider
.when('/Rights',{templateUrl:'views/Right/search.html', controller:'SearchRightController', resolve: { load: function () {loadcontroller("right/searchRightController");}}})
.otherwise({
redirectTo: '/'
});
}])
Advertisements

Hard Working & Commitment matter

Eager to learn new things, facing problem are key to development power. Hard working, Dedication, Commitment can ship great product. Listening others, accepting different views, ideas can increase creativity. Helping mind can sharp knowledge. Small small idea can bring another idea. Exploring new things open new door of opportunity. Over the 5 years of software development experience, Now I feel these.

 

As a Software engineer, I worked several platform. I believe, platform depends on project and personal choice. I worked in Java, .Net, Php. Interestingly, now-a-days most of the platform following same pattern and trends. That’s why, It’s very easy to adapt with new technologies.

 

Earlier In my career, I worked web development where server side component used like JSF, JSP etc. Now, AngularJS won developers heart. Because, client side should not depend on server side. Seriously, Restful API is responsible for more cutting edge, excellent web application project.

However, I always believe faith is key of success. At the same time, Hard working and strong commitment can bring success with faith.

 

I want to engage some exciting project where I can meet gig people. Toptal is great place to work on exciting project. I want to be a part of  Toptal community.