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.

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:

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:

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.

Originally published by  Michael Good at dzone.com

==========================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Angular 8 (formerly Angular 2) - The Complete Guide

☞ Complete Angular 8 from Zero to Hero | Get Hired

☞ Learn and Understand AngularJS

☞ The Complete Angular Course: Beginner to Advanced

☞ Angular Crash Course for Busy Developers

☞ Angular Essentials (Angular 2+ with TypeScript)

☞ Angular (Full App) with Angular Material, Angularfire & NgRx

☞ Angular & NodeJS - The MEAN Stack Guide


angular web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Wondering how to upgrade your skills in the pandemic? Here's a simple way you can do it.

Corona Virus Pandemic has brought the world to a standstill. Countries are on a major lockdown. Schools, colleges, theatres, gym, clubs, and all other public

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.