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.

 

Configure mysql datasource in wildfly 8.0

Add mysql module to wildfly application server:

1. Go to wildfly-8.0.0.Final\modules\system\layers\base\com
2. Create folder like “sql\mysql\main\” and go to sql\mysql\main\
3. Place mysql connector jar to main folder
4. Place module.xml file to main folder

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.sql.mysql">
  <resources>
     <resource-root path="mysql-connector-java-5.0.4-bin.jar"/>              
  </resources>
  <dependencies>
     <module name="javax.api"/>
  </dependencies>
</module>

Add datasource to wildfly configuration file

1. Go to C:\Program Files\wildfly-8.0.0.Final\wildfly-8.0.0.Final\standalone\configuration
2. Open standalone.xml file (we assume you are using standalone.xml as server configuration file)
3. Add following code to datasources section

<datasources>
	<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
		<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
		<driver>h2</driver>
		<security>
			<user-name>sa</user-name>
			<password>sa</password>
		</security>
	</datasource>
	<datasource jndi-name="java:jboss/datasources/brachub" pool-name="brachub" enabled="true" use-java-context="true">
		<connection-url>jdbc:mysql://localhost:3306/brachub</connection-url>
		<driver>mysql</driver>
		<security>
			<user-name>root</user-name>
			<password>1234</password>
		</security>
		<statement>
			<prepared-statement-cache-size>100</prepared-statement-cache-size>
			<share-prepared-statements>true</share-prepared-statements>
		</statement>
	</datasource>
	<drivers>
		<driver name="mysql" module="com.sql.mysql">
			<driver-class>com.mysql.jdbc.Driver</driver-class>
		</driver>
		<driver name="h2" module="com.h2database.h2">
			<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
		</driver>
	</drivers>
</datasources>

Hope this will work.