Angularjs manual Analytic expression ($parse) _javascript techniques

Source: Internet
Author: User


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!


Related Article

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.