The beast's angular Api learning, translating and understanding-$interpolate and $parse

Source: Internet
Author: User

ng API Learning for Beasts--$interpolate and $parse

$interpolate

Compiles a string into an interpolation function. The HTML compilation service uses this service to complete data binding.

The $interpolate comes with an optional fourth parameter, allornothing. If Allornothing is true, the interpolation function will return undefined unless all embedded expressions are evaluated as not undefined.

Using code:

<div ng-app= "Demo" >

<div ng-controller= "Democtrl" >

<input ng-model= "Text2"/>

{{Value}}

</div>

</div>

<script>

Angular.module (' Demo ', [])

. Controller ("Democtrl", ["$scope", "$interpolate", Function ($scope, $interpolate) {

var obj = {text1: "Hello", Text2: "World"};

$scope. Text2 = "World";

$scope. $watch ("Text2", function (N,o) {

var interpolate = $interpolate ("{{Text1}}" +n);

$scope. Value = interpolate (obj);

})

}])

</script>

$parse

Converts an angular expression to a function.

Use: $parse (expression);

Expression: The expressions that are compiled.

Return: Represents the compiled function of an expression. function (context,locals)

Context: An object that contains an expression (usually a scope object) in the statement that needs to be parsed.

Locals: Object, the context object of the local variable, used to override the value in the context.

The returned function contains a property:

Literal:boolean, whether the top node of the expression is a JavaScript literal.

Constant:boolean, whether the expression is all composed of JavaScript constant literals.

Assign:function (context,locals), which can be used to modify the value of an expression in a given context.

Using code:

Angular.module (' Demo ', [])

. Controller ("Democtrl", ["$scope", "$parse", Function ($scope, $parse) {

var obj = {

Text: "World"

};

var parses = $parse ("' Hello ' + text");

var v = parses (obj);//Hello World

}])

This follow-up to do add ...

The beast's angular Api learning, translating and understanding-$interpolate and $parse

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.