此博客是本人从学生时代开始做笔记所用, 部分是工作所遇问题,做填坑笔记,部分闲来查阅资料,加上自己的理解所总结的学习笔记, 常忙得不可开交,若漏了资料来源,望通知~ 前路漫漫,写点东西告诉自己正在一点点进步,而不要迷失于繁忙。

用angular实现$.param()

首先介绍一下$.param()

功能: 序列化对象或数组,返回字符串

eg:

var params = { width:1900, height:1200 };
var str = jQuery.param(params);
console.log(str);

输出: width=1680&height=1050

使用angular替代,方法为:

function serializeData( data ) { 
    // If this is not an object, defer to native stringification.
    if ( ! angular.isObject( data ) ) { 
        return( ( data == null ) ? "" : data.toString() ); 
    }            
    var buffer = [];            
    // Serialize each key in the object.
    for ( var name in data ) { 
        if ( ! data.hasOwnProperty( name ) ) { 
            continue; 
        }            
        var value = data[ name ];            
        buffer.push(
            encodeURIComponent( name ) + "=" + encodeURIComponent( ( value == null ) ? "" : value )
        ); 
    }            
    // Serialize the buffer and clean it up for transportation.
    var source = buffer.join( "&" ).replace( /%20/g, "+" ); 
    return( source ); 
};

 

posted @ 2016-11-28 17:54  炎泽  阅读(2566)  评论(0编辑  收藏  举报