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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Rediscovering Angular: Modern Advantages and Technical Stack
  • Angular Input/Output Signals: The New Component Communication
  • Azure Deployment Using FileZilla
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators

Trending

  • Simpler Data Transfer Objects With Java Records
  • Scaling Azure Microservices for Holiday Peak Traffic Using Automated CI/CD Pipelines and Cost Optimization
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • Strategies for Securing E-Commerce Applications
  1. DZone
  2. Coding
  3. Frameworks
  4. Angular Scope Tutorial

Angular Scope Tutorial

The Scope model is an important concept in Angular. Let's take a look at what it is and how to make use of it.

By 
Michael Good user avatar
Michael Good
·
Jan. 22, 19 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
18.0K Views

Join the DZone community and get the full member experience.

Join For Free

1. Overview

In this post, we are reviewing scope in Angular. It is passed as an argument when we make a controller:

<script>
var myApp = angular.module('myApp', []);

myApp.controller('myController', function($scope) {
    $scope.name = "Red Dead Redemption";
});
</script>



Scope is actually an object that refers to the model in an application structure, such as Model View Controller (MVC) .

It provides definitions – also known as context – for JavaScript-like code snippets called expressions. Scopes are structured in a hierarchy that mimics the Document Object Model (DOM) structure of the application. Scopes can watch expressions and propagate events in a similar way to DOM events.

2. Scope Is a Data Model

What does it mean for Scope to be a data model? It is a JavaScript object with properties and methods that can be accessed by both the view and controller:

diagram showing how $scope object is accessed in angularjs

Here, we have an example that demonstrates how modifying the view can affect the controller and model:

<!DOCTYPE html>
<html>
   <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js"></script>
<body>

<div ng-app="demoApp" ng-controller="demoCtrl">

<input ng-model="word">

<h1>Hello, {{word}}!</h1>

</div>

<script>
var app = angular.module('demoApp', []);
app.controller('demoCtrl', function($scope) {
    $scope.word = "word";
});
</script>

<p>If we change the word in the input field, the change will affect the model and the word property in the controller.</p>

</body>
</html>

Feel free to copy and paste that code into your favorite text editor or download the file from my GitHub.

In this simple example, if we modify the input in the web page, we see the value change for the greeting in the <h1> tags:

3. Root Scope and Hierarchies

Each Angular application has only one root scope but can have any number of child scopes.

An application can have several scopes because directives can create new child scopes. When new scopes are created, they become children of their parent scope. This creates a tree structure which parallels the DOM where they’re attached.

The example below, which is available in my GitHub, shows how multiple scopes work in an application and also prototypal inheritance of properties:

<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js"></script>
  <script>
  (function(angular) {
  'use strict';
angular.module('scopeExample', [])
  .controller('HelloController', ['$scope', '$rootScope', function($scope, $rootScope) {
    $scope.name = 'World';
    $rootScope.department = 'Red Dead Redemption 2';
  }])
  .controller('ListController', ['$scope', function($scope) {
    $scope.names = ['Arthur', 'Dutch', 'Bill'];
  }]);
})(window.angular);

  </script>

</head>
<body ng-app="scopeExample">
  <div class="show-scope-demo">
  <div ng-controller="HelloController">
    Hello {{name}}!
  </div>
  <div ng-controller="ListController">
    <ol>
      <li ng-repeat="name in names">{{name}} from {{department}}</li>
    </ol>
  </div>
</div>

The page will display like this:

Multiple scope example in AngularJS. Shows prototypal inheritance.

In the code shown above, when [[name]] is evaluated by Angular, it first looks at the scope associated with the value for the ng-controller attribute for the name property. This is why it says “Hello World!” on top rather than a character name from Red Dead Redemption 2.

If the property is not found, it searches the parent scope and so on until the root scope is reached. The Angular documentation calls this prototypical inheritance, while others like Mozilla call it prototypal inheritance.

Regardless of what it’s truly called, what we need to know is JavaScript only has one construct: objects. Every object has a private property that holds a link to another object called its prototype.

4. Conclusion

Today we reviewed the core concepts of scope in Angular. We reviewed that it is an object that resides in the model of an application, that it is hierarchical and has prototypal inheritance, and, there can only be one root scope.

Angular can be used effectively with the Spring Framework. To learn more Angular, I will be checking out courses on Udemy and Treehouse.

AngularJS

Published at DZone with permission of Michael Good, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Rediscovering Angular: Modern Advantages and Technical Stack
  • Angular Input/Output Signals: The New Component Communication
  • Azure Deployment Using FileZilla
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!