zoukankan      html  css  js  c++  java
  • 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');
          };
        });
  • 相关阅读:
    [Linux] day04——Linux 入门
    react 资源汇总
    画原型图工具
    Atom 插件安装
    react 编写组件 五
    webstom 配置git 后左侧菜单栏配色调整
    Webstorm 不识别es6 import React from ‘react’——webstorm不支持jsx语法怎么办
    Es6 之for of
    一个react的完整项目展示
    前后端分离 接口管理神器——Rap本地搭建
  • 原文地址:https://www.cnblogs.com/boshen-hzb/p/9234587.html
Copyright © 2011-2022 走看看