前面了解了AngularJS的基本用法,這里就跟著PDF一起學習下表達式的相關內容。
在AngularJS中的表達式,與js中并不完全相同。
首先它的表達式要放在{{}}才能使用,其次相對于javascript中的表達式概念,它有以下幾點不同:
1 作用域不同
在javascript中默認的作用于是window,但是在angularJs中就不同了。它使用$scope控制作用于。
2 允許未定義的值
在angularjs中,如果使用了未定義的表達式,也不會出現錯誤,直接返回空值。
3 過濾器
可以在表達式中使用 | 管道命令符,添加過濾器,與UNIX的命令行類似。
4 $符號
用以區別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}}
運行結果:


以上就是對AngularJS 表達式的資料整理,后續繼續補充相關資料,謝謝大家的支持!
新聞熱點
疑難解答