码迷,mamicode.com
首页 > Web开发 > 详细

angularjs写公共方法

时间:2018-06-27 16:47:38      阅读:1074      评论:0      收藏:0      [点我收藏+]

标签:str   $scope   service   object   ict   res   class   调用   pre   

‘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写公共方法

标签:str   $scope   service   object   ict   res   class   调用   pre   

原文地址:https://www.cnblogs.com/boshen-hzb/p/9234587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!