Turn: Talk about the $parse and $eval in Angularjs

Source: Internet
Author: User

Talk about $parse and $eval in Angularjs.

Angularjs beginners often feel a little confused about the two built-in services of $parse and $eval, and today we'll talk about $parse and $eval in Angularjs.

In general, both $parse and $eval are expressions that act on ANGULARJS.

What is an expression?

The expressions in Angularjs are snippets of code that resemble JavaScript code (but they differ from JavaScript code). Expressions are often used to prevent the binding, for example {{expression}} . Here is an example:

1 + 2 = {{ 1 + 2 }}

Or:

{{ user.name }} 
$parse Services

$parse service can be used to convert an expression to a function. This function can be called, where the argument is a context object, which is generally scoped.

In addition, the function returned by the $parse expression has a assign property. This assign property is also a function that can be used to change the value of the expression in a given context.

Here's a simple code:

<div my-attr="obj.name" my-directive>testing</div>

App.directive(' Mydirective ',function($log, $parse){Returnfunction(Scope, Elem, Attrs){Parse "My-attr attribute value into a function"var model= $Parse(Attrs. myattr);Model is now a function that can be called to get the value of an expressionThe following line of code will output the value of Obj.name in the scope $log.Log(Model(Scope)); Elem.Bind(' Click ',function ({//' model.assign ' is also a function that is used to update the value of an expression Modelassign (Scope,< Span class= "token string" > ' New name '   Scope.$apply () }) }}     

The above example can fully reflect why we need $parse services. If the attribute value is name, then we can do it without $parse, only with scope[attrs.myattr]. But in the example above, square brackets do not work.

$eval Services

$eval is a method in scope scope that executes an expression in the current scope and returns the result:

scope.a = 1;scope.b = 2;scope.$eval(‘a+b‘); // 3 

Turn: Talk about the $parse and $eval in Angularjs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.