Home  >  Article  >  php教程  >  Detailed explanation of important instructions in angular ($eval, $parse and $compile)

Detailed explanation of important instructions in angular ($eval, $parse and $compile)

高洛峰
高洛峰Original
2016-12-09 13:59:081134browse

Among Angular's services, there are some services that you have to understand, because they can be said to be the core of ng. Today, I want to introduce the two core services of ng, $parse and $compile. In fact, a lot of people talk about these two services, but there are 100 Hamlets for every 100 readers. I am here to talk about my understanding of these two services.

You may have questions, $eval is not actually a service. It is a method in the scope and cannot be regarded as a service. Moreover, it is also based on parse, so it can only be regarded as another way of writing $parse. , let’s take a look at the definition of $eval in the ng source code and we’ll know

$eval: function(expr, locals) {
    return $parse(expr)(this, locals);
   },

I believe everyone will understand after reading the source code. Okay, now let’s start the explanation of the two core services. If you feel If what I said is wrong, please feel free to point it out in the comment area or private chat, so as not to harm other readers.

When I talk about these two services, I will first talk about a concept in this post: context

I believe that many people have heard of this "context", but it may be a bit vague. Let me explain it to you here. Let’s see if everyone accepts this statement.

Do you still remember angular’s ​​data binding? For example: I now have a controller called TestCtrl, and its content is as follows:

.controller('TestCtrl', function($scope) {
      $scope.test = "Boo!!!"
  })

And in html our code is like this

<body ng-controller="TestCtrl">
  {{test}}
</body>

Well, everyone knows it without thinking As a result, the words Boo!!! will definitely be displayed on the page.

But what if I delete the ng-controller directive? That is to say, I did not declare the controller in html. What will happen if you bind {{test}} directly?

There is only one result, that is, there is nothing on the page (ps: because you declared ng-app). Do you understand this?

The controller is equivalent to a context container. The real context is actually $scope. When the page is bound to test, if a controller is declared, the current context is the $scope in the controller. ng will look for your control. Does the context $scope of the controller have a test? If so, it will of course be displayed, but what about when you don’t declare the controller? His context container is ng-app, then his real context is $rootScope. At this time, he will look for $rootScope to see if there is a test.

Okay, we have finished talking about the concept of context. It is actually quite easy to understand. It is basically very similar to this

So let’s get down to business, let’s start talking about $parse. The first thing we need to look at is ng’s API documentation

var getter = $parse(&#39;user.name&#39;);
var setter = getter.assign;
var context = {user:{name:&#39;angular&#39;}};
var locals = {user:{name:&#39;local&#39;}};
 
expect(getter(context)).toEqual(&#39;angular&#39;);
setter(context, &#39;newValue&#39;);
expect(context.user.name).toEqual(&#39;newValue&#39;);
expect(getter(context, locals)).toEqual(&#39;local&#39;);

What you see are the most cost-effective lines of code for the $parse service in the ng document.

getter and setter are the well-known get method and set method. context and locals are just json objects, the purpose is to simulate Contextual relationship

The following four statements that you see can eventually pass the test. Now let’s analyze them one by one. Before the analysis, I have to explain what $parse is.

The $parse service is actually a function of parsing expressions. Just like ng-model="test", when you write this in HTML, who knows that in ng-model="test", in fact, what you want to bind is in the scope (context) of the current controller (context container) For the value in test, ng uses the $parse service to help you parse the expression, so when calling the $parse service, you need to pass the context object to let ng know where you want to go to find you. This test.

So we see the first line of test code is like this:

getter(context)).toEqual(&#39;angular&#39;) //实际上就是 $parse(&#39;user.name&#39;)(context)

In this context, it is the context. The principle of returning the "angular" string is through these three steps:

1. Get the current expression user.name

2. Get the current context object {user:{name:'angular'}}

3. Find the expression in the upper and lower object, and finally get the character "angular" created

So this test code is successful.

Let’s look at the second method, the setter method

setter(context, &#39;newValue&#39;);//实际上就是 $parse(&#39;user.name&#39;).assign(context, &#39;newValue&#39;)
expect(context.user.name).toEqual(&#39;newValue&#39;);//测试数据上下文的值是否被改变

The setter method here is actually a change value method

1. Get the current expression user.name

2. Get the current context object {user: {name:'angular'}}

3. Change the value in the expression and program the context object {user:{name:'newValue'}}

Then the context object changes and reuse the getter method to get the expression expression, the context has been changed from {user:{name:'angular'}} --> {user:{name:'newValue'}}, and the value of the expression finally obtained is naturally "newValue", so the test The code also passed.

expect(getter(context, locals)).toEqual(&#39;local&#39;);//实际上就是$parse(&#39;user.name&#39;)(context, locals)

What we want to show here is actually the context replacement function.

In the getter method, we can not only select the first context, but if we pass the second parameter, the first context will be overwritten by the second context. Note that it is overwritten.

1. Get the current context The expression of user.name

2. Get the current context object {user:{name:'angular'}}

3. Overwrite the current context {user:{name:'local'}}

4. Get The value of the expression after parsing

Back to $eval, we can see from the $eval source code that $eval only has a get function and no set function, but sometimes we can choose to pass a second context to achieve the effect of modifying the value.

The $parse service has been finished here, and the next step is $compile

--------------------------------- --------------------

If you understand the concept of $parse, I think $compile is almost understood. In fact, it is very similar to $parse. But it parses a piece of html code, and its function is to turn a dead template into a live template, which is also the core service of the command.

For example, if you have a piece of html code 4a249f0d628e2318394fd9b75b4636b1{{test}}473f0a7621bec819994bb5020d29372a, if you put this code directly in the html code, what content will it display? I won’t say that everyone should also Understand. This is a dead template, and the so-called live template means that all the data in it has been bound by data. {{test}} will automatically find the current context to bind the data. The last thing displayed is the live template, that is, the template that has been data bound.

$compile('dead template') (context object), in this way, the dead template is programmed into a live template, and you can perform operations on this live html code, such as adding it to the current node, etc.

But in the instruction, she will return two functions pre-link and post-link

The first one executed is pre-link. Its traversal order for the same instruction is from the parent node to the child node. At this stage, the DOM node has not yet stabilized and cannot perform some binding event operations, but we can perform some initialization data processing here.

The second execution is post-link, which is what we often call the link function. It traverses from child nodes to parent nodes. At this stage, the DOM node has stabilized, and we usually do a lot of work here. operation.


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