# AngularJS – How to Use AngularJS with Legacy UI Code?

This article represents tips on how to use AngularJS with legacy UI code which could present challenges such as those described later in this article. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following are some of the key points described later in this article:

• Is it that easy to create Angular apps in Legacy UI?
• Can I use AngularJS with existing forms?
• Is it OK to use Angular with JQuery-based UIs?
• Browser compatibility, especially, for IE-sensitive applications

###### Is it that easy to create Angular apps with Legacy UI Code?

Yes, it is! All you need to do is following:

• Include one or more angularjs scripts: You could use  AngularJS scripts from Google Hosted Libraries to get started quickly without having the need to download the Angular scripts. Following is sample code to include core angular.min.js.
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>

• Define angular app using ng-app directive: As we are talking about working with existing/legacy UIs, following are two different scenarios to define your angular app using ng-app directive:
• Create an angular app in the existing UI page. For this, all you need to do is include ng-app=”” directive within selective div element and get started. Well, you could as well define ng-app on HTML element. However, to keep it neat, it may be a good idea to define it on selective div element that becomes your angular app. Note that one page can contain just one ng-app directive. This must be kept in mind while choosing “div” element to define ng-app. You could then include ng-controller directives on same div element or one or more inner div elements. Following are code samples:
<!-- Model 1: ng-app and ng-controller on same div element -->
<div ng-app="HelloApp" ng-controller="HelloCtrl">
</div>
<script type="text/javascript">
angular.module('HelloApp', [])
.controller('HelloCtrl', ['$scope', function($scope){

}])
</script>

<!-- Model 2: ng-controller defined on inner div elements -->
<div ng-app="HelloApp">
<div ng-controller="HelloCtrl1">

</div>
<div ng-controller="HelloCtrl2">

</div>
</div>
<script type="text/javascript">
angular.module('HelloApp', [])
.controller('HelloCtrl1', ['$scope', function($scope){

}])
.controller('HelloCtrl2', ['$scope', function($scope){

}])
</script>


• Create angular apps for new UIs: For all new UIs, it is same as creating angular app with new application. You could use ng-app directly on html element or on any div element you wish.
• Add one or more controllers to the angular apps: Once created the angular app, you could include one or more controllers using ng-controller directives. Take a look at the code sample given above which demonstrates two different ways in which you could define controllers within an angular app. Also, given is the javascript code which would be required to work with these controllers.

###### Can I use AngularJS with existing forms?

Yes, AngularJS could be used with existing forms in some of the following ways:

• Define an ng-app and an ng-controller on the form’s parent, maybe, a div element.
• Bind one or more form inputs with models using ng-model directive
• If required, bind the form inputs with one or more functions such as ng-click/ng-change etc, to capture the event
• Define the controller code to capture the events and execute business logic
• And, that is it!

###### Is is OK to Use Angular with JQuery-based UIs?

Well, this may be of interest to many UI developers who often have been found wondering on whether it would be conflict if one uses AngularJS with JQuery. At times, they have been found to be saying that AngularJS is altogether a different world than JQuery. Well, let me present this fact that AngularJS does make use of lighter version of JQuery which is called as JQLite. Following code is executed during the bootstrap of angular apps. Pay attention to the call of bindJquery() method. It actually tries to bind JQuery. Thus, if you already have used JQuery on the page, it binds JQLite variable with JQuery. Otherwise, it binds JQLite, a lighter version of JQuery which comes packed with AngularJS core script.

	if (window.angular.bootstrap) {
console.log('WARNING: Tried to load angular more than once.');
return;
}

//try to bind to jquery now so that one can write jqLite(document).ready()
//but we will rebind on bootstrap again.
bindJQuery();

publishExternalAPI(angular);

angularInit(document, bootstrap);
});

})(window, document);


Thus, the baseline is, it does not impact at all, if you have made use of JQuery in your existing UI. You could simply create Angular apps based on instructions described above in the article.

###### Browser compatibility, especially, for IE-sensitive applications

Well, AngularJS 1.2.* is supported for IE 8 browser. However, from 1.3 release onwards, IE 8 will not be supported. So, if your application needs to be supported on IE 8 in time to come, and you are very keen on using AngularJS, you may have to live with AngularJS 1.2.* versions until you decide to stop supporting IE 8, thereby moving to upcoming Angular 1.3.*.