AngularJS Tutorial: Introduction to AngularJS Scopes

Introduction to AngularJS Scopes

In the previous tutorial of this series we learned how to test controllers using Jasmine and Karma. In this post we discussed how data is passed from controller to view using $scope, which is the glue that links model and view. In this tutorial we’ll talk more about scopes, what are some possible pitfalls in using them, and later we’ll talk about the ways to overcome possible problems, as well as get over Batarang, a Chrome extension from AngularJS team, which can be used to analyze scopes inside an AngularJS application.

By adding the ng-app directive to some HTML tag we specify that an AngularJS will be created and in the process of application creation Angular creates the so-called root scope. Some other directives such as ng-controller create a child scope. To visualize scopes one can use a Google Chrome extension called Batarang. The picture below shows the root scope for the AngularJS application and the child scope created by HelloController, which contains a single variable myModel.

What if we created another controller, say to work not with greetings but farewells and that controller would contain a variable with the same name as in HelloController? It is possible to have  variables with the same name that belong to different scopes. The snippet below shows the code for our GoodbyeController. While variables have the same name, the content is different.

Now we should add another section to our index.html file and attach a controller to it as shown below.

It should be noted that it’s necessary to add the link to the controller’s code to the HTML file. The source code for this tutorial can be found here. Now we have two scopes inside the root scope, each of which was created by one of the two controllers, which is depicted in the following images.

The myModel variable in the scope of the first controller has the value ‘World’ and the value ‘Everyone’ in the scope of the second controller.

When we write tests for our second controller, the code is pretty much the same as in the first case, but things are different with e2e tests because we extracted elements by their model and now we have two models with the same name. To solve this problem one should replace a piece of code that finds a single element


by the code that extracts the list of elements with the same model name and then specify the position of the desired element in the list.


Nested AngularJS Scopes

A more complex case is shown by the following snippet of code when we have an article nested in a section and each of them has its own controller.

In each of the controllers we have variables with the same name, myModel. The code for the parent controller is shown below.

Also, each controller has a variable specific to it. The following snippet show the code for a child or nested controller.

Now we can use Batarang to explore scopes. We see that the myModel variable in the parent scope has the value different than in the child scope as shown by the screenshots below.

Also, we have access to the variable created in the parent scope inside the child scope as can be seen from the web page.

The explanation of the fact that we have access to a variable created in the parent scope from inside the child scope is that parent and child scope are linked with JavaScript Prototypal Inheritance relationship, which means that when the onlyParent variable is not found in the child scope, JavaScript looks for it in the parent, parent of a parent, etc. scopes.

The question is whether we can access the value of the myModel variable defined in the parent scope from the child scope. The answer is yes and this can be accomplished by the snippet of code shown below.


The takeaway from this post is that AngularJS scopes are difficult to work with because AngularJS directives may create their own scopes and variables defined in a parent scope can be hidden by the ones defined in child scopes. This can be error prone but there are some best practices that help one to avoid this kind of problems.


In this tutorial we talked about scope creation and how to visualize scopes using a Chrome extension called Batarang. Also, we talked about nested scopes and to what problems that could lead as well as how to do end-to-end testing in this case. In the next tutorial we’ll talk about the best practices that allow to avoid problems with AngularJS nested scopes.


  1. JavaScript Inheritance and the Prototype Chain


  1. Webtrackker technology is the best IT training institute in NCR. Webtrackker provide training on all latest technology such as java training. Webtrackker is not only training institute but also it also provide best IT solution to his client. Webtrackker provide training by experienced and working in the industry on same technology.Webtrackker Technology C-67 Sector-63 Noida 8802820025

    Java Training institute in indirapuram

    Java training institute in Noida

    Java training institute in Ghaziabad

    java training institute in Vaishali

    Java Training institute in Vasundhara

    Java Training institute in Delhi South Ex

  2. Pretty section of content. I simply stumbled upon your site and in accession capital to say that I get actually loved to account your blog posts.
    Housekeeping Services in Chennai

  3. nice post for sharing Java article. its really helpful for me. keep shring on updated tutorials????????????????


  4. Come and read us!! We are the No 1 AngularJS Training providers in Chennai. We are teaching how to be AngularJS Ninjas!!! That's right! - Our AngularJS Course Training was designed in a way which helps both freshers and professionals. Book your FREE DEMO Session now @ Credo Systemz angularjs training

  5. This comment has been removed by the author.

  6. This comment has been removed by the author.

  7. Nice information and good example about the angularjs . I will fully understand the scope of the Angularjs. you provide the information is very nice and super. Thanks for sharing.

    Angularjs Online Training


  8. I saw lot of information On Other Site But this blog helped me alot to learn Java Thanks for sharing.........

  9. Angular.js training is needed since its a bit complex to understand. I like the way you teach here. It makes things better and easier for us.

  10. Nice to know about the Introduction to AngularJS Scopes, Good Content with simple explanation Well The thing is when i was having my PMP Course in Pune I heard about AngularJS It has really a good scope in the market Thankyou for sharing

  11. The blog you wrote is the sign of a good writer. I am highly impressed with this blog, due to which the information here seems very beneficial to me.You are a very good writer and I am excited to read your new blog..

  12. Thanks for sharing this blog. AngularJS is a trending one in today's world. Your blog is really helpful, continue sharing more like this.
    AngularJS Training in Chennai | AngularJS Course in Chennai

  13. Hello, Well I am gland to see the tutorial of AngularJS, But i have a doubt in regards to the set of things relative to AngularJS Is AngularJS taken from JAVA? Should we have certain set of brief idea on JAVA and its updates or algorithms? Well When I was having my PMP Training in Bangalore I was able to understand certain objectives of AngularJS. Anywayz thankyou for sharing the information I will be more gland to know that wheather it is related to Java orelse anyother set of Domain Keep Updating Thank you

  14. hello,

    Nice article,Thank you so much...keep sharing this kind of information....

    Angular JS training in bangalore

  15. This is extremely great information for these blog. And Very good work. It is very interesting to learn from to easy considered. Thank you for giving information.

    Bigdata Training in Chennai

  16. I really appreciate the information shared above. It’s of great help. MaxMunus provides Remote Support For Corporate and for Individuals. If anyone is facing any issue in his project of # ANGULAR JS we can support them remotely , kindly contact us
    MaxMunus Offer World Class Industry best Consultant on# ANGULAR JS. We provide end to end Remote Support on Projects. MaxMunus is successfully doing remote support for countries like India, USA, UK, Australia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain, and UAE etc.
    Skype id: saurabhmaxmunus
    Ph:(0) 8553576305/ 080 - 41103383


Post a Comment

Popular posts from this blog

Getting started with Dropwizard: Connecting to a Database using Hibernate

Getting Started with Dropwizard: Authentication, Configuration and HTTPS

Getting started with Dropwizard: First steps