angularjs写公共方法
Posted 波神
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了angularjs写公共方法相关的知识,希望对你有一定的参考价值。
‘use strict‘; angular.module(‘fast-westone‘) .factory(‘commonUtilService‘, function () { return { /** * 将json转成form Data * @param params * @returns {string} */ transform: function (params) { var str = []; for (var p in params) { var a = params[p] ; if(!a && a!= false && a != 0 ) continue ; if (typeof params[p] === ‘object‘) { str.push(encodeURIComponent(p) + ‘=‘ + encodeURIComponent(angular.toJson(params[p]))); } else { str.push(encodeURIComponent(p) + ‘=‘ + encodeURIComponent(params[p])); } } return str.join(‘&‘); } } })
调用的地方:
"use strict"; angular.module("fast-westone").controller(‘blServerUpdateCtrl‘, function ($scope, blServerServiceOperation, toastr, $uibModal, $log, commonUtilService) { $scope.editServer.desc = $scope.getDesc($scope.orinalServer); var transform = function (params) { var str = []; for (var p in params) { var a = params[p] ; if(!a && a!= false && a != 0 ) continue ; if (typeof params[p] === ‘object‘) { str.push(encodeURIComponent(p) + ‘=‘ + encodeURIComponent(angular.toJson(params[p]))); } else { str.push(encodeURIComponent(p) + ‘=‘ + encodeURIComponent(params[p])); } } return str.join(‘&‘); }; /** * 保存 */ $scope.ok = function () { var paramObj = { name: $scope.editServer.name, desc: $scope.editServer.desc }; paramObj = commonUtilService.transform(paramObj); blServerServiceOperation.operate($scope.editServer.id, paramObj, ‘update‘).$promise.then(function (resp) { toastr.success(‘修改虚拟机成功‘); $scope.modal.dismiss(‘cancel‘); },function(error){ $log.error(error); toastr.success(‘修改虚拟机失败‘); }); }; /** * 取消 */ $scope.cancel = function () { $scope.modal.dismiss(‘cancel‘); }; });
以上是关于angularjs写公共方法的主要内容,如果未能解决你的问题,请参考以下文章