angularjs手动解析表达式($parse)

 1 <!DOCTYPE html>
 2 <html lang="zh-CN" ng-app="app">
 3 <head>
 4     <meta charset="utf-8">
 5     <title>手动解析表达式($parse)</title>
 6     <link rel="stylesheet" href="../bootstrap.min.js">
 7 </head>
 8 <body ng-controller="myController">
 9     <div ng-controller="myController">
10         <input type="text" ng-model="expr" placeholder="enter an expression">
11         <h2>{{ parsedValue }}</h2>
12     </div>
13     <script src="../angular.min.js"></script>
14     <script>
15         angular.module('app', [])
16         .controller('myController', function($scope, $parse) {
17             $scope.$watch('expr', function(newVal, oldVal, s) {
18                 if(newVal !== oldVal) {
19                     var parseFun = $parse(newVal);
20                     $scope.parsedValue = parseFun(s);
21                     // 你也可以这样写 
22                     // $scope.parsedValue = $parse(newVal)(s);
23                 }
24             })
25             
26         })
27     </script>
28 </body>
29 </html>

 

posted @ 2016-10-12 09:05  好来污影后  阅读(1768)  评论(0编辑  收藏  举报