Over a million developers have joined DZone.

AngularJS – How to Use AngularJS with Legacy UI Code?

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

This article presents 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 leave comment/suggest if I failed 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:

  1. 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.

  2. <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
  3. 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">
      <script type="text/javascript">
      angular.module('HelloApp', [])
      	.controller('HelloCtrl', ['$scope', function($scope){
      <!-- Model 2: ng-controller defined on inner div elements -->
      <div ng-app="HelloApp">
      	<div ng-controller="HelloCtrl1">
      	<div ng-controller="HelloCtrl2">
      <script type="text/javascript">
      angular.module('HelloApp', [])
      	.controller('HelloCtrl1', ['$scope', function($scope){
      	.controller('HelloCtrl2', ['$scope', function($scope){
    • 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.

  4. 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) {
    	//AngularJS is already loaded, so we can return here...
    	console.log('WARNING: Tried to load angular more than once.');
  	//try to bind to jquery now so that one can write jqLite(document).ready()
  	//but we will rebind on bootstrap again.
  	jqLite(document).ready(function() {
    	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.*.

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.


Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}