The following code describes the ANGULARJS manual parsing expression ($parse), as shown in the following code:
<!DOCTYPE html>
<html lang="zh-CN" ng-app="app">
<head>
<meta charset="utf-8">
< title > manually parse the expression ($parse) < / Title >
<link rel="stylesheet" href="../bootstrap.min.js">
</head>
<body ng-controller="myController">
<div ng-controller="myController">
<input type="text" ng-model="expr" placeholder="enter an expression">
<h2>{{ parsedValue }}</h2>
</div>
<script src="../angular.min.js"></script>
<script>
angular.module('app', [])
.controller('myController', function($scope, $parse) {
$scope.$watch('expr', function(newVal, oldVal, s) {
if(newVal !== oldVal) {
var parseFun = $parse(newVal);
$scope.parsedValue = parseFun(s);
//You can also write in this way
// $scope.parsedValue = $parse(newVal)(s);
}
}
}
</script>
</body>
</html>
Supplement: parsing angularjs expression
Add: Parse Angularjs expressions
Although Angularjs will automatically parse expressions during the run of the $digest loop, it is sometimes useful to manually parse the expression. Angularjs the operation of an expression by $parse this internal service, which accesses the scope currently in place. This process allows us to access the original JavaScript data and functions defined on the $scope. The $parse service is injected into the controller and then invoked to implement a manual parse expression. For example, if there is an input box on the page that is bound to the expr variable, as follows:
<div ng-controller= "Mycontroller" >
<input ng-model= "expr"
type= "text"
placeholder= "Enter an Expression "/>
We can set the expression of expr in Mycontroller and parse it $watch:
Angular.module ("myApp", [])
. Controller (' Mycontroller ',
function ($scope, $parse) {
$scope. $watch (' Expr ', function (newval, oldval, scope) {
if (newval!== oldval) {
///Use this expression to set Parsefun
var parsefun = $parse ( newval);
Gets the value of the parsed expression $scope.parsedvalue = Parsefun (scope);});
The above is a small series to introduce the ANGULARJS manual analytic Expression ($parse), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!