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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM