Explanation of scope scope in angularjs
This article mainly introduces the detailed explanation of the scope of angularjs learning. Now I will share it with you and give you a reference.
Introduction
Scope is the link between HTML (view) and JavaScript (controller).
Scope is an object that stores the application data model and has available methods and properties.
Scope can be applied to views and controllers.
Scope is the glue between the controller and the view of the web application:
Controller--> Scope--> View (DOM)
Command- -> Scope--> View (DOM)
When you create a controller in AngularJS, you can pass the $scope object as a parameter:
<p ng-controller="myCtrl"> <h1>{{name}}</h1> </p> <script> var app = angular.module('test', []); app.controller('myCtrl', function($scope) { $scope.name = "天下行走"; }); </script>
Output result: Walking around the world
Create an attribute name "name" in the controller, which corresponds to the name used in {{ }} in the view.
When the $scope object is added to the controller, the view (HTML) can obtain these properties.
In the view, you do not need to add the $scope prefix, you only need to add the attribute name, such as: {{name}}.
AngularJS application is composed as follows:
View (view), that is, HTML.
Model (model), the data available in the current view.
Controller (controller), which is a JavaScript function, can add or modify properties.
A scope is a JavaScript object with properties and methods that can be used in views and controllers.
Let’s look at another example:
<p ng-app="myApp" ng-controller="myCtrl"> 输入你的名字: <input ng-model="name"> <h1>{{greeting}}</h1> <button ng-click='sayHello()'>greet</button> </p> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.name = "张三"; $scope.sayHello = function() { $scope.greeting = $scope.name + '是个笨蛋!'; }; }); </script>
In this example,
Controller: MyCtrl, which references $scope and is registered on it It has two properties and one method
$scope object: holds the data model required for the above example, including name attribute and greeting attribute (Note: This is done in the sayHello() method registered when calling) and sayHello() methods
View: has an input box, a button and a content block that uses two-way binding to display data
The specific example has two processes, from the perspective of controller initiation:
1. The controller writes attributes to the scope:
To the role The name in the domain is assigned a value, and then the scope notifies the input data in the view that it has changed. Because input realizes two-way binding through ng-model, it can know the change in name, and then render the changed value in the view
2. The controller writes the method
in the scope to assign a value to the sayHello() method in the scope. This method is called by the button in the view, because the button is bound to this method through ng-click. When the user When the button is clicked, sayHello() is called. This method reads the name attribute in the scope, adds the suffix string, and then assigns it to the newly created greeting attribute in the scope.
The entire example process if From the perspective of the view, it mainly consists of the following three parts:
1. Rendering logic in input: shows the two-way binding of the scope through ng-model and a certain form element in the view
Go to the scope according to the name in ng-model. If there is already a value, then fill the current input box with this default value
Accept user input and pass the string entered by the user to name. At this time, the attribute value in the scope is updated in real time to the value entered by the user
2. Logic in button
Accept user clicks and call the sayHello() method in the scope
3, rendering logic of {{greeting}}
Initial stage: When the user does not click the button, the content will not be displayed
Value phase: After the user clicks, this expression will get the greeting attribute from the scope, and in this example this function The domain and the controller are the same. At this time, the greeting attribute under the scope already exists, and this attribute is retrieved.
Calculation phase: in the current scope Go down and calculate the greeting expression, and then render the view, showing that Zhang San is an idiot!
After analyzing the example process from the above two perspectives, we can know: The scope object and its properties are the only data source for view rendering.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
angularjs uses gulp-uglify to compress the execution error after execution solution
Vue ElementUI implements dynamic rendering and visualization of forms Configuration method
A brief discussion on the solution to excessively large files after webpack packaging
Actually solve the problem of UglifyJs error when packaging iview (detailed tutorial)
The problem of refreshing 404 after using vue to package the project (detailed tutorial)
The above is the detailed content of Explanation of scope scope in angularjs. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article continues the learning of Angular, takes you to understand the metadata and decorators in Angular, and briefly understands their usage. I hope it will be helpful to everyone!

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

Do you know Angular Universal? It can help the website provide better SEO support!

This article will share with you an Angular practical experience and learn how to quickly develop a backend system using angualr combined with ng-zorro. I hope it will be helpful to everyone!

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!
