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
我的名字是
我的年龄是
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:
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.