DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Find out today at 11 am ET in our "DZone 2023 Preview: DevOps Edition!"
Last chance to join
  1. DZone
  2. Coding
  3. Frameworks
  4. AngularJS – How to Use AngularJS with Legacy UI Code?

AngularJS – How to Use AngularJS with Legacy UI Code?

Ajitesh Kumar user avatar by
Ajitesh Kumar
CORE ·
Sep. 30, 14 · Interview
Like (0)
Save
Tweet
Share
7.98K Views

Join the DZone community and get the full member experience.

Join For Free

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">
      </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.

  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.');
    	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);
 
  	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.*.

AngularJS app Element JQuery

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Unleashing the Power of JavaScript Modules: A Beginner’s Guide
  • Top Five Tools for AI-based Test Automation
  • Type Variance in Java and Kotlin
  • Exploring the Benefits of Cloud Computing: From IaaS, PaaS, SaaS to Google Cloud, AWS, and Microsoft

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: