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