過濾器可使用一個管道字符(|)添加到表達式和指令中。javascript
AngularJS 過濾器可用於轉換數據:html
以currency 過濾器爲例:java
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript" src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controler="myCtrl"> 單價:<input type="number" ng-model="price"><br> 數量:<input type="number" ng-model="num"><br> <br> <p>總價:{{(price*num) | currency}}</p> </div> <script type="text/javascript"> var app = angular.module('myApp',[]); app.controller('myCtrl',function($scope){ $scope.price = 9.9; $scope.num = 1; }); </script> </body> </html>
注意:app
單價:<input type="number" ng-model="price"><br>
ng-model 指令用於綁定應用程序數據到 HTML 控制器(input, select, textarea)的值,這裏是ng-model而不是ng-module;spa
輸入類型爲number而不是text。
code
var app = angular.module('myApp',[]);
括號內部是單引號不是雙引號。htm
app.controller('myCtrl',function($scope)
不要把scope前面的$和function遺漏。ip