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 id="name">{{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 id="greeting">{{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!

JavaandJavaScriptaredistinctlanguages:Javaisusedforenterpriseandmobileapps,whileJavaScriptisforinteractivewebpages.1)Javaiscompiled,staticallytyped,andrunsonJVM.2)JavaScriptisinterpreted,dynamicallytyped,andrunsinbrowsersorNode.js.3)JavausesOOPwithcl

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools
