Angular 手动解析表达式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="js/angular.min.js"></script> <script> angular.module("myApp", []).controller('MyController', function($scope, $parse) { $scope.a = 'test' $scope.$watch('expr', function(newVal, oldVal, scope) { if (newVal !== oldVal) { // 用该表达式设置parseFun var parseFun = $parse(newVal) console.log($parse('a+2')(scope)) // 获取经过解析后表达式的值 $scope.parsedValue = parseFun(scope) } }) }) </script> </head> <body ng-app="myApp"> <div ng-controller="MyController"> <input ng-model="expr" type="text" placeholder="Enter an expression"> {{ parsedValue }} </div> </body> </html>