Tech Junkie Blog - Real World Tutorials, Happy Coding!: AngularJS SPA : Refactor Code to Not Use Global Variables (Bank App)

Sunday, June 26, 2016

AngularJS SPA : Refactor Code to Not Use Global Variables (Bank App)

In the previous blog post we got AngularJS to work with a module and a controller.  However, we put everything in the global scope.  The problem with that is that there are many JavaScript libraries that might be using the same variables as we are, or if we are working with other developers, there might be some unforeseen conflicts.  The way we can mitigate this problem is to wrap our modules and controllers in an anonymous function.  Think of an anonymous function as a wrapper or a container to hold our modules and controllers.  Another term developers like to refer to anonymous function is an IIFE.  Whatever it's called it's always good practice to avoid putting things in the global environment if it can be avoided.

Here are the steps to take the modules and controllers out of the global environment:

1.  First let's wrap the module in an anonymous function.  The source code for the app.js file should look like the following

    'use strict';

    var app = angular.module('bankApp',['bankController']);

The code above wraps the application in anonymous function and assigned to the variable call "app"

2.  Now open the "bankController.js" in the "controllers" folder and wrap the "bankController" in anonymous function, the source code for the file should look like the following

   'use strict';

  $scope.bankAppName="My Bank Application";

3.  Open up the "index.html" file and make sure the source code looks like the following.

<!DOCTYPE html>
<html lang="en">
 <meta charset="utf-8">
 <title>Bank Application</title>
 <link rel="stylesheet" type="text/css" href="app.css">
<body ng-app="bankApp" ng-controller="bankController">
<script type="text/javascript" src="js/lib/angular/angular.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/controllers/bankController.js"></script>

4.If you open the file you will see that application still works

In this tutorial we've taken some steps to future proof our application so that we are not working in the global environment


  1. Js has really been helpful 🙌🙌 thanks a lot

    1. Thank you! I tried to be as clear as possible. Sometimes books can be dry :)

  2. We share this js coding is really helpful to me. And it's this site content is good very useful to me.

  3. Отличная led лента находится только у Ekodio, даже не сомневайтесь в этом, я лично проверял.

  4. Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.

    SAP PP Training in Bangalore

    Best SAP PP Training Institutes in Bangalore

  5. I have recently visited your blog profile. I am totally impressed by your blogging skills and knowledge.

    SAP HANA Online Training

    SAP HANA Classes Online

    SAP HANA Training Online

    Online SAP HANA Course

    SAP HANA Course Online

  6. Hi Frankcompressors here, Blog is very interesting, thanks for sharing.

  7. I recently visited your blog profile. I was really impressed with blogging skills and knowledge.
    Vist our website too, We are Screw Dilator suppliers.

  8. Really amazing experience to visit your blog, thanks a lot for sharing with us. Visit Ogen Infosystem for creative website designing and ppc services at best price in Delhi.
    Web Designing Company

  9. Water bodies are the main source of transportation for international freight forwarding. Due to this, sea freight company in Delhi,
    Freight Forwarder in Vietnam
    Shipping Company In India


Search This Blog