


A brief discussion on the detailed explanation of the scope attribute of AngularJs instructions
AngularJS uses the directive() method class to define a directive:
.directive("name",function(){ return{ }; })
The above is the main framework for defining a directive. This method accepts two parameters:
1. The first parameter: name represents the defined directive. Name (angularjs will use this name to register this command)
2. The second parameter: function. The sweet potato must return an object or a function, but usually we will return an object. What follows return is the returned object.
There is a scope attribute in the returned object, which is used to modify the scope of the instruction.
Every time when registering a directive, this directive will consider a question: Should we use my own scope, or create a child scope that inherits from the parent scope,
or create an isolated scope (Does not rely on external scope); The values of the
scope attribute are false, true, and { } respectively correspond to the above scope, subscope, and isolation scope.
Let’s give three examples to fully understand the usage of these attributes.
1. scope: false
html code
<div ng-controller="myController"> <div> <span>我的名字是</span><span ng-bind="name"></span><br/> <span>我的年龄是</span><span ng-bind="age"></span> <div my-directive></div> </div> </div> <script> angular.module("app",[]) .controller("myController",function($scope){ $scope.name = "kobe"; $scope.age = 39; }) .directive("myDirective",function(){ return{ scope:false, restrict:"A", template:"<div><h1 id="下面的部分是我们创建指令时生成的">下面的部分是我们创建指令时生成的</h1>"+ "我的名字是:<span ng-bind='name'></span><br/>"+ "我的年龄是:<span ng-bind='age'></span><br/>"+ "输入你的新名字:<input type='text' ng-model='name'>"+ "</div>" }; }) </script>
Effect:
At this time, the scope of the command is the same as the scope of myController, so when we enter a new name in the input box When, the names in the upper and lower places will change accordingly, as shown in the figure below:
2. scope: true
The effect of just entering the page at this time, when we enter a new name in the input box, the same as the first one will happen The results of each experiment are different, as shown in the figure:
Here, the names in the upper part have not changed, but the names in the lower part have changed.
In this experiment, there are two points that we need to pay attention to:
1. After just entering or refreshing the page, the names of the upper and lower scopes are the same because: when the scope is true, the child scope inherits the attributes of the parent scope and method. Therefore, although name and age are not defined in the child scope, they can be inherited from myController in the parent scope. Therefore, the same name and age are shown above and below.
2. After entering a new name, the upper part remains unchanged and the lower part changes. The reason is: we are modifying the name and age on the child scope, so the lower name will change. The upper name belongs to the parent scope, and the parent scope Subscopes cannot be accessed,
so the value of the above name will not change.
3. Scope: { }
The scope part of the command is modified as follows:
scope:{ name:"@", age:"@" },
We will find that the following names and ages have no values. At this time, because the current scope is isolated, it does not know the parent role Domain properties and methods. We can assign a value to it after the instruction as follows:
<div my-directive name="aaa" age="33"></div>
For the same reason, this scope is completely isolated, so the modification is only effective for the attributes and methods of the scope of the instruction itself and has nothing to do with any other scopes , the name of myController scope will not change.

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

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.

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
