-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-$parse.html
29 lines (29 loc) · 1.23 KB
/
angular-$parse.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>angular-$parse</title>
<script type="text/javascript" src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="app" ng-controller="MyController">
<input type="text" ng-model="expression" placeholder="enter a expression">
<div>The result is {{result}}</div>
<script type="text/javascript">
var app = angular.module('app', []);
app.controller('MyController', ['$scope', '$parse', '$timeout', function($scope, $parse, $timeout){
$timeout(function(){
$scope.expression = '1+1';
}, 1000);
$scope.$watch('expression', function(newValue, oldValue, scope){
if(newValue != oldValue){
// 手动解析表达式
// var 解析后的函数 = $parse(表达式)
var parseFn = $parse(newValue);
// 执行解析后的函数
$scope.result = parseFn(scope, null);
}
});
}]);
</script>
</body>
</html>