前面瞭解了AngularJS的基本用法,這裏就跟着PDF一塊兒學習下表達式的相關內容。javascript
在AngularJS中的表達式,與js中並不徹底相同。html
首先它的表達式要放在{{}}才能使用,其次相對於javascript中的表達式概念,它有如下幾點不一樣:java
1 做用域不一樣angularjs
在javascript中默認的做用因而window,可是在angularJs中就不一樣了。它使用$scope控制做用於。app
2 容許未定義的值學習
在angularjs中,若是使用了未定義的表達式,也不會出現錯誤,直接返回空值。ui
3 過濾器spa
能夠在表達式中使用 | 管道命令符,添加過濾器,與UNIX的命令行相似。命令行
4 $符號code
用以區別angular的方法與用戶自定義的方法。
下面看一段小代碼:
<!doctype html> <html ng-app> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script> </head> <body> <div ng-controller="ctl"> name:<input ng-model="name" type="text"> <button ng-click="reset()">reset</button> <br> {{name}} <br> hello ! {{test}} <br> filter : {{name | uppercase}} </div> <script type="text/javascript"> function ctl($scope){ var str = "init"; $scope.name = str; $scope.reset = function(){ $scope.name = str; } } </script> </body> </html>
經過reset觸發reset方法,重置name變量的內容;
在表達式中,引用了未定義的test,可是並無報錯,直接默認顯示爲空;—— {{test}}
最後使用過濾器,將表達式中name的值轉化成大寫。—— {{name | uppercase}}
運行結果: