用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 );
};
上一篇:WebService学习总结——调用第三方提供的webService服务


下一篇:Java中replace与replaceAll区别