AngularJS Scope
source link: https://www.tuicool.com/articles/hit/NziIZbb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
1. Overview
In this post, we are reviewing scope in AngularJS. 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 & Hierarchies
Each AngularJS 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 AngularJS, 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 AngularJS 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 AngularJS. 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.
AngularJS can be used effectively with theSpring Framework. To learn more AngularJS, I will be checking out courses on Udemy and Treehouse .
I started my website in 2017 to help people learn more about information technology in order to help them become more successful in their careers. I earned my bachelor’s degree from New York University, have been working in cyber security since 2012, and live in the Washington D.C. Metro area.
I havefound running a blog to be one of the greatest assets in helping my career and have found it could even bring in a few extra bucks each month.Starting a blog is easy and rewarding.
To keep my knowledge current, I take courses on Udemy and Treehouse . By using my links, you can get a discount on both.
Recommend
-
104
一款优秀的前端框架——AngularJS 前 言 AngularJS是一款为了克服HT...
-
129
前言 对于AngularJS什么,小编在这就不多做介绍了。大家可以看小编的上一篇博客。 言归正传,小编在上一篇博客中介绍了AngularJS中的指令、表达式还有非常实用的三种服务...
-
104
PHP Tutorial with AngularJS 1.6+
-
129
Progressively migrating from AngularJS to Vue.js at Unbabel
-
98
AngularJS is planning one more significant release, version 1.7, and on July 1, 2018 it will enter a 3 year Long Term Support period. AngularJS is an extremely stable framework for building web…
-
64
AngularJS和Angular的路线图
-
56
这篇是对angularJS的一些疑点回顾,是对目前angularJS开发的各种常见问题的整理汇总。如果对文中的题目全部了然于胸,觉得对整个angular框架应该掌握的七七八八了。希望志同道合的通知补充内容
-
2
AngularJS receives a selected element in its scope advertisements I'm developing app with Ionic and AngularJS. Can't figure how to get value o...
-
3
[AngularJS] Update Property in Parent Scope Updated: February 23, 2015 ...
-
5
[AngularJS] Safe Scope.$apply Implementation (Error: $apply already in progress) Updated: February 23,...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK