Home  >  Article  >  php教程  >  A brief discussion on the detailed explanation of the scope attribute of AngularJs instructions

A brief discussion on the detailed explanation of the scope attribute of AngularJs instructions

高洛峰
高洛峰Original
2016-12-09 11:01:281296browse

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>下面的部分是我们创建指令时生成的</h1>"+
            "我的名字是:<span ng-bind=&#39;name&#39;></span><br/>"+
            "我的年龄是:<span ng-bind=&#39;age&#39;></span><br/>"+
            "输入你的新名字:<input type=&#39;text&#39; ng-model=&#39;name&#39;>"+
            "</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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn