部分知識來源html
1.先自定義過濾器web
var pmsApp = angular.module("pmsApp", ["ui.router", 'ui.bootstrap', 'ui.bootstrap.pagination','ngCookies','pascalprecht.translate'],function ($httpProvider) { $httpProvider.defaults.headers.post['Content-Type'] = 'application/json'; $httpProvider.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest'; }); pmsApp.filter('sumColumn',function(){ return function(data, key){ if(typeof(data) === undefined || typeof(key) === undefined ) { return 0; } var sum = 0, i = data.length - 1; for(; i >= 0; i--) { sum += parseFloat(data[i][key] ? data[i][key] : 0); } return sum.toFixed(2); }; });
2.在視圖上使用過濾器:ajax
{{obj | filterName:參數1:參數2:...}} //obj是要過濾的對象
3.在控制器裏面使用:
首先需注入$filter以後才能使用,具體用法以下:json
$filter(filterName)(要過濾的對象,參數1,參數2,...)
注:bootstrap
$httpProvider.defaults.headers.post['Content-Type'] = 'application/json'; $httpProvider.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';
能夠設置Content-Type 爲 application/json ,可是若是ajax請求中參數傳遞不正確,仍是有可能會報Content-Type的錯誤,好比
Content type 'application/octet-stream' not supported ,因此須要檢查參數傳遞問題app
有關AngularJS代碼不符合語法,致使報錯ide
toFixed() 方法可把 Number 四捨五入爲指定小數位數的數字。post
var sum2 = (parseFloat(a)+parseFloat(b)+parseFloat(c)).toFixed(4)
語法NumberObject.toFixed(num)參數描述num必需。規定小數的位數,是 0 ~ 20 之間的值,包括 0 和 20,有些實現能夠支持更大的數值範圍。若是省略了該參數,將用 0 代替。ui
調用全局方法,獲取返回值,給當前controller賦值.net
// 供應商分類 $rootScope.getSupplierClassificationArray(function(result){ $scope.supplierClassificationArray = result; $scope.queryContent.supplierType = result.data[0].classNode; }); // 履約狀況 $rootScope.performStatusArray(function(result){ $scope.performStatusSelect = result; $scope.queryContent.performStatus = result[0].value; });
view代碼 (設置value和text)
<select class="form-control" ng-model="queryContent.performStatus"> <option ng-repeat="performStatus in performStatusSelect" value="{{performStatus.value}}"> {{performStatus.text}} </option> </select>
注: 設置默認選中第一個正確方式爲
$scope.queryContent.performStatus = result[0].value; // √ $scope.queryContent.performStatus = result[0]; // ×